system-bus.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * PS3 system bus driver.
  4. *
  5. * Copyright (C) 2006 Sony Computer Entertainment Inc.
  6. * Copyright 2006 Sony Corp.
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/init.h>
  10. #include <linux/export.h>
  11. #include <linux/dma-map-ops.h>
  12. #include <linux/err.h>
  13. #include <linux/slab.h>
  14. #include <asm/udbg.h>
  15. #include <asm/lv1call.h>
  16. #include <asm/firmware.h>
  17. #include <asm/cell-regs.h>
  18. #include "platform.h"
  19. static struct device ps3_system_bus = {
  20. .init_name = "ps3_system",
  21. };
  22. /* FIXME: need device usage counters! */
  23. static struct {
  24. struct mutex mutex;
  25. int sb_11; /* usb 0 */
  26. int sb_12; /* usb 0 */
  27. int gpu;
  28. } usage_hack;
  29. static int ps3_is_device(struct ps3_system_bus_device *dev, u64 bus_id,
  30. u64 dev_id)
  31. {
  32. return dev->bus_id == bus_id && dev->dev_id == dev_id;
  33. }
  34. static int ps3_open_hv_device_sb(struct ps3_system_bus_device *dev)
  35. {
  36. int result;
  37. BUG_ON(!dev->bus_id);
  38. mutex_lock(&usage_hack.mutex);
  39. if (ps3_is_device(dev, 1, 1)) {
  40. usage_hack.sb_11++;
  41. if (usage_hack.sb_11 > 1) {
  42. result = 0;
  43. goto done;
  44. }
  45. }
  46. if (ps3_is_device(dev, 1, 2)) {
  47. usage_hack.sb_12++;
  48. if (usage_hack.sb_12 > 1) {
  49. result = 0;
  50. goto done;
  51. }
  52. }
  53. result = lv1_open_device(dev->bus_id, dev->dev_id, 0);
  54. if (result) {
  55. pr_warn("%s:%d: lv1_open_device dev=%u.%u(%s) failed: %s\n",
  56. __func__, __LINE__, dev->match_id, dev->match_sub_id,
  57. dev_name(&dev->core), ps3_result(result));
  58. result = -EPERM;
  59. }
  60. done:
  61. mutex_unlock(&usage_hack.mutex);
  62. return result;
  63. }
  64. static int ps3_close_hv_device_sb(struct ps3_system_bus_device *dev)
  65. {
  66. int result;
  67. BUG_ON(!dev->bus_id);
  68. mutex_lock(&usage_hack.mutex);
  69. if (ps3_is_device(dev, 1, 1)) {
  70. usage_hack.sb_11--;
  71. if (usage_hack.sb_11) {
  72. result = 0;
  73. goto done;
  74. }
  75. }
  76. if (ps3_is_device(dev, 1, 2)) {
  77. usage_hack.sb_12--;
  78. if (usage_hack.sb_12) {
  79. result = 0;
  80. goto done;
  81. }
  82. }
  83. result = lv1_close_device(dev->bus_id, dev->dev_id);
  84. BUG_ON(result);
  85. done:
  86. mutex_unlock(&usage_hack.mutex);
  87. return result;
  88. }
  89. static int ps3_open_hv_device_gpu(struct ps3_system_bus_device *dev)
  90. {
  91. int result;
  92. mutex_lock(&usage_hack.mutex);
  93. usage_hack.gpu++;
  94. if (usage_hack.gpu > 1) {
  95. result = 0;
  96. goto done;
  97. }
  98. result = lv1_gpu_open(0);
  99. if (result) {
  100. pr_warn("%s:%d: lv1_gpu_open failed: %s\n", __func__,
  101. __LINE__, ps3_result(result));
  102. result = -EPERM;
  103. }
  104. done:
  105. mutex_unlock(&usage_hack.mutex);
  106. return result;
  107. }
  108. static int ps3_close_hv_device_gpu(struct ps3_system_bus_device *dev)
  109. {
  110. int result;
  111. mutex_lock(&usage_hack.mutex);
  112. usage_hack.gpu--;
  113. if (usage_hack.gpu) {
  114. result = 0;
  115. goto done;
  116. }
  117. result = lv1_gpu_close();
  118. BUG_ON(result);
  119. done:
  120. mutex_unlock(&usage_hack.mutex);
  121. return result;
  122. }
  123. int ps3_open_hv_device(struct ps3_system_bus_device *dev)
  124. {
  125. BUG_ON(!dev);
  126. pr_debug("%s:%d: match_id: %u\n", __func__, __LINE__, dev->match_id);
  127. switch (dev->match_id) {
  128. case PS3_MATCH_ID_EHCI:
  129. case PS3_MATCH_ID_OHCI:
  130. case PS3_MATCH_ID_GELIC:
  131. case PS3_MATCH_ID_STOR_DISK:
  132. case PS3_MATCH_ID_STOR_ROM:
  133. case PS3_MATCH_ID_STOR_FLASH:
  134. return ps3_open_hv_device_sb(dev);
  135. case PS3_MATCH_ID_SOUND:
  136. case PS3_MATCH_ID_GPU:
  137. return ps3_open_hv_device_gpu(dev);
  138. case PS3_MATCH_ID_AV_SETTINGS:
  139. case PS3_MATCH_ID_SYSTEM_MANAGER:
  140. pr_debug("%s:%d: unsupported match_id: %u\n", __func__,
  141. __LINE__, dev->match_id);
  142. pr_debug("%s:%d: bus_id: %llu\n", __func__, __LINE__,
  143. dev->bus_id);
  144. BUG();
  145. return -EINVAL;
  146. default:
  147. break;
  148. }
  149. pr_debug("%s:%d: unknown match_id: %u\n", __func__, __LINE__,
  150. dev->match_id);
  151. BUG();
  152. return -ENODEV;
  153. }
  154. EXPORT_SYMBOL_GPL(ps3_open_hv_device);
  155. int ps3_close_hv_device(struct ps3_system_bus_device *dev)
  156. {
  157. BUG_ON(!dev);
  158. pr_debug("%s:%d: match_id: %u\n", __func__, __LINE__, dev->match_id);
  159. switch (dev->match_id) {
  160. case PS3_MATCH_ID_EHCI:
  161. case PS3_MATCH_ID_OHCI:
  162. case PS3_MATCH_ID_GELIC:
  163. case PS3_MATCH_ID_STOR_DISK:
  164. case PS3_MATCH_ID_STOR_ROM:
  165. case PS3_MATCH_ID_STOR_FLASH:
  166. return ps3_close_hv_device_sb(dev);
  167. case PS3_MATCH_ID_SOUND:
  168. case PS3_MATCH_ID_GPU:
  169. return ps3_close_hv_device_gpu(dev);
  170. case PS3_MATCH_ID_AV_SETTINGS:
  171. case PS3_MATCH_ID_SYSTEM_MANAGER:
  172. pr_debug("%s:%d: unsupported match_id: %u\n", __func__,
  173. __LINE__, dev->match_id);
  174. pr_debug("%s:%d: bus_id: %llu\n", __func__, __LINE__,
  175. dev->bus_id);
  176. BUG();
  177. return -EINVAL;
  178. default:
  179. break;
  180. }
  181. pr_debug("%s:%d: unknown match_id: %u\n", __func__, __LINE__,
  182. dev->match_id);
  183. BUG();
  184. return -ENODEV;
  185. }
  186. EXPORT_SYMBOL_GPL(ps3_close_hv_device);
  187. #define dump_mmio_region(_a) _dump_mmio_region(_a, __func__, __LINE__)
  188. static void _dump_mmio_region(const struct ps3_mmio_region* r,
  189. const char* func, int line)
  190. {
  191. pr_debug("%s:%d: dev %llu:%llu\n", func, line, r->dev->bus_id,
  192. r->dev->dev_id);
  193. pr_debug("%s:%d: bus_addr %lxh\n", func, line, r->bus_addr);
  194. pr_debug("%s:%d: len %lxh\n", func, line, r->len);
  195. pr_debug("%s:%d: lpar_addr %lxh\n", func, line, r->lpar_addr);
  196. }
  197. static int ps3_sb_mmio_region_create(struct ps3_mmio_region *r)
  198. {
  199. int result;
  200. u64 lpar_addr;
  201. result = lv1_map_device_mmio_region(r->dev->bus_id, r->dev->dev_id,
  202. r->bus_addr, r->len, r->page_size, &lpar_addr);
  203. r->lpar_addr = lpar_addr;
  204. if (result) {
  205. pr_debug("%s:%d: lv1_map_device_mmio_region failed: %s\n",
  206. __func__, __LINE__, ps3_result(result));
  207. r->lpar_addr = 0;
  208. }
  209. dump_mmio_region(r);
  210. return result;
  211. }
  212. static int ps3_ioc0_mmio_region_create(struct ps3_mmio_region *r)
  213. {
  214. /* device specific; do nothing currently */
  215. return 0;
  216. }
  217. int ps3_mmio_region_create(struct ps3_mmio_region *r)
  218. {
  219. return r->mmio_ops->create(r);
  220. }
  221. EXPORT_SYMBOL_GPL(ps3_mmio_region_create);
  222. static int ps3_sb_free_mmio_region(struct ps3_mmio_region *r)
  223. {
  224. int result;
  225. dump_mmio_region(r);
  226. result = lv1_unmap_device_mmio_region(r->dev->bus_id, r->dev->dev_id,
  227. r->lpar_addr);
  228. if (result)
  229. pr_debug("%s:%d: lv1_unmap_device_mmio_region failed: %s\n",
  230. __func__, __LINE__, ps3_result(result));
  231. r->lpar_addr = 0;
  232. return result;
  233. }
  234. static int ps3_ioc0_free_mmio_region(struct ps3_mmio_region *r)
  235. {
  236. /* device specific; do nothing currently */
  237. return 0;
  238. }
  239. int ps3_free_mmio_region(struct ps3_mmio_region *r)
  240. {
  241. return r->mmio_ops->free(r);
  242. }
  243. EXPORT_SYMBOL_GPL(ps3_free_mmio_region);
  244. static const struct ps3_mmio_region_ops ps3_mmio_sb_region_ops = {
  245. .create = ps3_sb_mmio_region_create,
  246. .free = ps3_sb_free_mmio_region
  247. };
  248. static const struct ps3_mmio_region_ops ps3_mmio_ioc0_region_ops = {
  249. .create = ps3_ioc0_mmio_region_create,
  250. .free = ps3_ioc0_free_mmio_region
  251. };
  252. int ps3_mmio_region_init(struct ps3_system_bus_device *dev,
  253. struct ps3_mmio_region *r, unsigned long bus_addr, unsigned long len,
  254. enum ps3_mmio_page_size page_size)
  255. {
  256. r->dev = dev;
  257. r->bus_addr = bus_addr;
  258. r->len = len;
  259. r->page_size = page_size;
  260. switch (dev->dev_type) {
  261. case PS3_DEVICE_TYPE_SB:
  262. r->mmio_ops = &ps3_mmio_sb_region_ops;
  263. break;
  264. case PS3_DEVICE_TYPE_IOC0:
  265. r->mmio_ops = &ps3_mmio_ioc0_region_ops;
  266. break;
  267. default:
  268. BUG();
  269. return -EINVAL;
  270. }
  271. return 0;
  272. }
  273. EXPORT_SYMBOL_GPL(ps3_mmio_region_init);
  274. static int ps3_system_bus_match(struct device *_dev,
  275. struct device_driver *_drv)
  276. {
  277. int result;
  278. struct ps3_system_bus_driver *drv = ps3_drv_to_system_bus_drv(_drv);
  279. struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
  280. if (!dev->match_sub_id)
  281. result = dev->match_id == drv->match_id;
  282. else
  283. result = dev->match_sub_id == drv->match_sub_id &&
  284. dev->match_id == drv->match_id;
  285. if (result)
  286. pr_info("%s:%d: dev=%u.%u(%s), drv=%u.%u(%s): match\n",
  287. __func__, __LINE__,
  288. dev->match_id, dev->match_sub_id, dev_name(&dev->core),
  289. drv->match_id, drv->match_sub_id, drv->core.name);
  290. else
  291. pr_debug("%s:%d: dev=%u.%u(%s), drv=%u.%u(%s): miss\n",
  292. __func__, __LINE__,
  293. dev->match_id, dev->match_sub_id, dev_name(&dev->core),
  294. drv->match_id, drv->match_sub_id, drv->core.name);
  295. return result;
  296. }
  297. static int ps3_system_bus_probe(struct device *_dev)
  298. {
  299. int result = 0;
  300. struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
  301. struct ps3_system_bus_driver *drv;
  302. BUG_ON(!dev);
  303. dev_dbg(_dev, "%s:%d\n", __func__, __LINE__);
  304. drv = ps3_system_bus_dev_to_system_bus_drv(dev);
  305. BUG_ON(!drv);
  306. if (drv->probe)
  307. result = drv->probe(dev);
  308. else
  309. pr_debug("%s:%d: %s no probe method\n", __func__, __LINE__,
  310. dev_name(&dev->core));
  311. pr_debug(" <- %s:%d: %s\n", __func__, __LINE__, dev_name(&dev->core));
  312. return result;
  313. }
  314. static void ps3_system_bus_remove(struct device *_dev)
  315. {
  316. struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
  317. struct ps3_system_bus_driver *drv;
  318. BUG_ON(!dev);
  319. dev_dbg(_dev, "%s:%d\n", __func__, __LINE__);
  320. drv = ps3_system_bus_dev_to_system_bus_drv(dev);
  321. BUG_ON(!drv);
  322. if (drv->remove)
  323. drv->remove(dev);
  324. else
  325. dev_dbg(&dev->core, "%s:%d %s: no remove method\n",
  326. __func__, __LINE__, drv->core.name);
  327. pr_debug(" <- %s:%d: %s\n", __func__, __LINE__, dev_name(&dev->core));
  328. }
  329. static void ps3_system_bus_shutdown(struct device *_dev)
  330. {
  331. struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
  332. struct ps3_system_bus_driver *drv;
  333. BUG_ON(!dev);
  334. dev_dbg(&dev->core, " -> %s:%d: match_id %d\n", __func__, __LINE__,
  335. dev->match_id);
  336. if (!dev->core.driver) {
  337. dev_dbg(&dev->core, "%s:%d: no driver bound\n", __func__,
  338. __LINE__);
  339. return;
  340. }
  341. drv = ps3_system_bus_dev_to_system_bus_drv(dev);
  342. BUG_ON(!drv);
  343. dev_dbg(&dev->core, "%s:%d: %s -> %s\n", __func__, __LINE__,
  344. dev_name(&dev->core), drv->core.name);
  345. if (drv->shutdown)
  346. drv->shutdown(dev);
  347. else if (drv->remove) {
  348. dev_dbg(&dev->core, "%s:%d %s: no shutdown, calling remove\n",
  349. __func__, __LINE__, drv->core.name);
  350. drv->remove(dev);
  351. } else {
  352. dev_dbg(&dev->core, "%s:%d %s: no shutdown method\n",
  353. __func__, __LINE__, drv->core.name);
  354. BUG();
  355. }
  356. dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
  357. }
  358. static int ps3_system_bus_uevent(struct device *_dev, struct kobj_uevent_env *env)
  359. {
  360. struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
  361. if (add_uevent_var(env, "MODALIAS=ps3:%d:%d", dev->match_id,
  362. dev->match_sub_id))
  363. return -ENOMEM;
  364. return 0;
  365. }
  366. static ssize_t modalias_show(struct device *_dev, struct device_attribute *a,
  367. char *buf)
  368. {
  369. struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
  370. int len = snprintf(buf, PAGE_SIZE, "ps3:%d:%d\n", dev->match_id,
  371. dev->match_sub_id);
  372. return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
  373. }
  374. static DEVICE_ATTR_RO(modalias);
  375. static struct attribute *ps3_system_bus_dev_attrs[] = {
  376. &dev_attr_modalias.attr,
  377. NULL,
  378. };
  379. ATTRIBUTE_GROUPS(ps3_system_bus_dev);
  380. struct bus_type ps3_system_bus_type = {
  381. .name = "ps3_system_bus",
  382. .match = ps3_system_bus_match,
  383. .uevent = ps3_system_bus_uevent,
  384. .probe = ps3_system_bus_probe,
  385. .remove = ps3_system_bus_remove,
  386. .shutdown = ps3_system_bus_shutdown,
  387. .dev_groups = ps3_system_bus_dev_groups,
  388. };
  389. static int __init ps3_system_bus_init(void)
  390. {
  391. int result;
  392. if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
  393. return -ENODEV;
  394. pr_debug(" -> %s:%d\n", __func__, __LINE__);
  395. mutex_init(&usage_hack.mutex);
  396. result = device_register(&ps3_system_bus);
  397. BUG_ON(result);
  398. result = bus_register(&ps3_system_bus_type);
  399. BUG_ON(result);
  400. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  401. return result;
  402. }
  403. core_initcall(ps3_system_bus_init);
  404. /* Allocates a contiguous real buffer and creates mappings over it.
  405. * Returns the virtual address of the buffer and sets dma_handle
  406. * to the dma address (mapping) of the first page.
  407. */
  408. static void * ps3_alloc_coherent(struct device *_dev, size_t size,
  409. dma_addr_t *dma_handle, gfp_t flag,
  410. unsigned long attrs)
  411. {
  412. int result;
  413. struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
  414. unsigned long virt_addr;
  415. flag &= ~(__GFP_DMA | __GFP_HIGHMEM);
  416. flag |= __GFP_ZERO;
  417. virt_addr = __get_free_pages(flag, get_order(size));
  418. if (!virt_addr) {
  419. pr_debug("%s:%d: get_free_pages failed\n", __func__, __LINE__);
  420. goto clean_none;
  421. }
  422. result = ps3_dma_map(dev->d_region, virt_addr, size, dma_handle,
  423. CBE_IOPTE_PP_W | CBE_IOPTE_PP_R |
  424. CBE_IOPTE_SO_RW | CBE_IOPTE_M);
  425. if (result) {
  426. pr_debug("%s:%d: ps3_dma_map failed (%d)\n",
  427. __func__, __LINE__, result);
  428. BUG_ON("check region type");
  429. goto clean_alloc;
  430. }
  431. return (void*)virt_addr;
  432. clean_alloc:
  433. free_pages(virt_addr, get_order(size));
  434. clean_none:
  435. dma_handle = NULL;
  436. return NULL;
  437. }
  438. static void ps3_free_coherent(struct device *_dev, size_t size, void *vaddr,
  439. dma_addr_t dma_handle, unsigned long attrs)
  440. {
  441. struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
  442. ps3_dma_unmap(dev->d_region, dma_handle, size);
  443. free_pages((unsigned long)vaddr, get_order(size));
  444. }
  445. /* Creates TCEs for a user provided buffer. The user buffer must be
  446. * contiguous real kernel storage (not vmalloc). The address passed here
  447. * comprises a page address and offset into that page. The dma_addr_t
  448. * returned will point to the same byte within the page as was passed in.
  449. */
  450. static dma_addr_t ps3_sb_map_page(struct device *_dev, struct page *page,
  451. unsigned long offset, size_t size, enum dma_data_direction direction,
  452. unsigned long attrs)
  453. {
  454. struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
  455. int result;
  456. dma_addr_t bus_addr;
  457. void *ptr = page_address(page) + offset;
  458. result = ps3_dma_map(dev->d_region, (unsigned long)ptr, size,
  459. &bus_addr,
  460. CBE_IOPTE_PP_R | CBE_IOPTE_PP_W |
  461. CBE_IOPTE_SO_RW | CBE_IOPTE_M);
  462. if (result) {
  463. pr_debug("%s:%d: ps3_dma_map failed (%d)\n",
  464. __func__, __LINE__, result);
  465. }
  466. return bus_addr;
  467. }
  468. static dma_addr_t ps3_ioc0_map_page(struct device *_dev, struct page *page,
  469. unsigned long offset, size_t size,
  470. enum dma_data_direction direction,
  471. unsigned long attrs)
  472. {
  473. struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
  474. int result;
  475. dma_addr_t bus_addr;
  476. u64 iopte_flag;
  477. void *ptr = page_address(page) + offset;
  478. iopte_flag = CBE_IOPTE_M;
  479. switch (direction) {
  480. case DMA_BIDIRECTIONAL:
  481. iopte_flag |= CBE_IOPTE_PP_R | CBE_IOPTE_PP_W | CBE_IOPTE_SO_RW;
  482. break;
  483. case DMA_TO_DEVICE:
  484. iopte_flag |= CBE_IOPTE_PP_R | CBE_IOPTE_SO_R;
  485. break;
  486. case DMA_FROM_DEVICE:
  487. iopte_flag |= CBE_IOPTE_PP_W | CBE_IOPTE_SO_RW;
  488. break;
  489. default:
  490. /* not happened */
  491. BUG();
  492. }
  493. result = ps3_dma_map(dev->d_region, (unsigned long)ptr, size,
  494. &bus_addr, iopte_flag);
  495. if (result) {
  496. pr_debug("%s:%d: ps3_dma_map failed (%d)\n",
  497. __func__, __LINE__, result);
  498. }
  499. return bus_addr;
  500. }
  501. static void ps3_unmap_page(struct device *_dev, dma_addr_t dma_addr,
  502. size_t size, enum dma_data_direction direction, unsigned long attrs)
  503. {
  504. struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
  505. int result;
  506. result = ps3_dma_unmap(dev->d_region, dma_addr, size);
  507. if (result) {
  508. pr_debug("%s:%d: ps3_dma_unmap failed (%d)\n",
  509. __func__, __LINE__, result);
  510. }
  511. }
  512. static int ps3_sb_map_sg(struct device *_dev, struct scatterlist *sgl,
  513. int nents, enum dma_data_direction direction, unsigned long attrs)
  514. {
  515. #if defined(CONFIG_PS3_DYNAMIC_DMA)
  516. BUG_ON("do");
  517. return -EPERM;
  518. #else
  519. struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
  520. struct scatterlist *sg;
  521. int i;
  522. for_each_sg(sgl, sg, nents, i) {
  523. int result = ps3_dma_map(dev->d_region, sg_phys(sg),
  524. sg->length, &sg->dma_address, 0);
  525. if (result) {
  526. pr_debug("%s:%d: ps3_dma_map failed (%d)\n",
  527. __func__, __LINE__, result);
  528. return -EINVAL;
  529. }
  530. sg->dma_length = sg->length;
  531. }
  532. return nents;
  533. #endif
  534. }
  535. static int ps3_ioc0_map_sg(struct device *_dev, struct scatterlist *sg,
  536. int nents,
  537. enum dma_data_direction direction,
  538. unsigned long attrs)
  539. {
  540. BUG();
  541. return -EINVAL;
  542. }
  543. static void ps3_sb_unmap_sg(struct device *_dev, struct scatterlist *sg,
  544. int nents, enum dma_data_direction direction, unsigned long attrs)
  545. {
  546. #if defined(CONFIG_PS3_DYNAMIC_DMA)
  547. BUG_ON("do");
  548. #endif
  549. }
  550. static void ps3_ioc0_unmap_sg(struct device *_dev, struct scatterlist *sg,
  551. int nents, enum dma_data_direction direction,
  552. unsigned long attrs)
  553. {
  554. BUG();
  555. }
  556. static int ps3_dma_supported(struct device *_dev, u64 mask)
  557. {
  558. return mask >= DMA_BIT_MASK(32);
  559. }
  560. static const struct dma_map_ops ps3_sb_dma_ops = {
  561. .alloc = ps3_alloc_coherent,
  562. .free = ps3_free_coherent,
  563. .map_sg = ps3_sb_map_sg,
  564. .unmap_sg = ps3_sb_unmap_sg,
  565. .dma_supported = ps3_dma_supported,
  566. .map_page = ps3_sb_map_page,
  567. .unmap_page = ps3_unmap_page,
  568. .mmap = dma_common_mmap,
  569. .get_sgtable = dma_common_get_sgtable,
  570. .alloc_pages = dma_common_alloc_pages,
  571. .free_pages = dma_common_free_pages,
  572. };
  573. static const struct dma_map_ops ps3_ioc0_dma_ops = {
  574. .alloc = ps3_alloc_coherent,
  575. .free = ps3_free_coherent,
  576. .map_sg = ps3_ioc0_map_sg,
  577. .unmap_sg = ps3_ioc0_unmap_sg,
  578. .dma_supported = ps3_dma_supported,
  579. .map_page = ps3_ioc0_map_page,
  580. .unmap_page = ps3_unmap_page,
  581. .mmap = dma_common_mmap,
  582. .get_sgtable = dma_common_get_sgtable,
  583. .alloc_pages = dma_common_alloc_pages,
  584. .free_pages = dma_common_free_pages,
  585. };
  586. /**
  587. * ps3_system_bus_release_device - remove a device from the system bus
  588. */
  589. static void ps3_system_bus_release_device(struct device *_dev)
  590. {
  591. struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
  592. kfree(dev);
  593. }
  594. /**
  595. * ps3_system_bus_device_register - add a device to the system bus
  596. *
  597. * ps3_system_bus_device_register() expects the dev object to be allocated
  598. * dynamically by the caller. The system bus takes ownership of the dev
  599. * object and frees the object in ps3_system_bus_release_device().
  600. */
  601. int ps3_system_bus_device_register(struct ps3_system_bus_device *dev)
  602. {
  603. int result;
  604. static unsigned int dev_ioc0_count;
  605. static unsigned int dev_sb_count;
  606. static unsigned int dev_vuart_count;
  607. static unsigned int dev_lpm_count;
  608. if (!dev->core.parent)
  609. dev->core.parent = &ps3_system_bus;
  610. dev->core.bus = &ps3_system_bus_type;
  611. dev->core.release = ps3_system_bus_release_device;
  612. switch (dev->dev_type) {
  613. case PS3_DEVICE_TYPE_IOC0:
  614. dev->core.dma_ops = &ps3_ioc0_dma_ops;
  615. dev_set_name(&dev->core, "ioc0_%02x", ++dev_ioc0_count);
  616. break;
  617. case PS3_DEVICE_TYPE_SB:
  618. dev->core.dma_ops = &ps3_sb_dma_ops;
  619. dev_set_name(&dev->core, "sb_%02x", ++dev_sb_count);
  620. break;
  621. case PS3_DEVICE_TYPE_VUART:
  622. dev_set_name(&dev->core, "vuart_%02x", ++dev_vuart_count);
  623. break;
  624. case PS3_DEVICE_TYPE_LPM:
  625. dev_set_name(&dev->core, "lpm_%02x", ++dev_lpm_count);
  626. break;
  627. default:
  628. BUG();
  629. }
  630. dev->core.of_node = NULL;
  631. set_dev_node(&dev->core, 0);
  632. pr_debug("%s:%d add %s\n", __func__, __LINE__, dev_name(&dev->core));
  633. result = device_register(&dev->core);
  634. return result;
  635. }
  636. EXPORT_SYMBOL_GPL(ps3_system_bus_device_register);
  637. int ps3_system_bus_driver_register(struct ps3_system_bus_driver *drv)
  638. {
  639. int result;
  640. pr_debug(" -> %s:%d: %s\n", __func__, __LINE__, drv->core.name);
  641. if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
  642. return -ENODEV;
  643. drv->core.bus = &ps3_system_bus_type;
  644. result = driver_register(&drv->core);
  645. pr_debug(" <- %s:%d: %s\n", __func__, __LINE__, drv->core.name);
  646. return result;
  647. }
  648. EXPORT_SYMBOL_GPL(ps3_system_bus_driver_register);
  649. void ps3_system_bus_driver_unregister(struct ps3_system_bus_driver *drv)
  650. {
  651. pr_debug(" -> %s:%d: %s\n", __func__, __LINE__, drv->core.name);
  652. driver_unregister(&drv->core);
  653. pr_debug(" <- %s:%d: %s\n", __func__, __LINE__, drv->core.name);
  654. }
  655. EXPORT_SYMBOL_GPL(ps3_system_bus_driver_unregister);