1 // SPDX-License-Identifier: GPL-2.0-only
3 * PS3 system bus driver.
5 * Copyright (C) 2006 Sony Computer Entertainment Inc.
6 * Copyright 2006 Sony Corp.
9 #include <linux/kernel.h>
10 #include <linux/init.h>
11 #include <linux/export.h>
12 #include <linux/dma-map-ops.h>
13 #include <linux/err.h>
14 #include <linux/slab.h>
17 #include <asm/lv1call.h>
18 #include <asm/firmware.h>
19 #include <asm/cell-regs.h>
23 static struct device ps3_system_bus = {
24 .init_name = "ps3_system",
27 /* FIXME: need device usage counters! */
30 int sb_11; /* usb 0 */
31 int sb_12; /* usb 0 */
35 static int ps3_is_device(struct ps3_system_bus_device *dev, u64 bus_id,
38 return dev->bus_id == bus_id && dev->dev_id == dev_id;
41 static int ps3_open_hv_device_sb(struct ps3_system_bus_device *dev)
46 mutex_lock(&usage_hack.mutex);
48 if (ps3_is_device(dev, 1, 1)) {
50 if (usage_hack.sb_11 > 1) {
56 if (ps3_is_device(dev, 1, 2)) {
58 if (usage_hack.sb_12 > 1) {
64 result = lv1_open_device(dev->bus_id, dev->dev_id, 0);
67 pr_warn("%s:%d: lv1_open_device dev=%u.%u(%s) failed: %s\n",
68 __func__, __LINE__, dev->match_id, dev->match_sub_id,
69 dev_name(&dev->core), ps3_result(result));
74 mutex_unlock(&usage_hack.mutex);
78 static int ps3_close_hv_device_sb(struct ps3_system_bus_device *dev)
83 mutex_lock(&usage_hack.mutex);
85 if (ps3_is_device(dev, 1, 1)) {
87 if (usage_hack.sb_11) {
93 if (ps3_is_device(dev, 1, 2)) {
95 if (usage_hack.sb_12) {
101 result = lv1_close_device(dev->bus_id, dev->dev_id);
105 mutex_unlock(&usage_hack.mutex);
109 static int ps3_open_hv_device_gpu(struct ps3_system_bus_device *dev)
113 mutex_lock(&usage_hack.mutex);
116 if (usage_hack.gpu > 1) {
121 result = lv1_gpu_open(0);
124 pr_warn("%s:%d: lv1_gpu_open failed: %s\n", __func__,
125 __LINE__, ps3_result(result));
130 mutex_unlock(&usage_hack.mutex);
134 static int ps3_close_hv_device_gpu(struct ps3_system_bus_device *dev)
138 mutex_lock(&usage_hack.mutex);
141 if (usage_hack.gpu) {
146 result = lv1_gpu_close();
150 mutex_unlock(&usage_hack.mutex);
154 int ps3_open_hv_device(struct ps3_system_bus_device *dev)
157 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_open_hv_device_sb(dev);
168 case PS3_MATCH_ID_SOUND:
169 case PS3_MATCH_ID_GPU:
170 return ps3_open_hv_device_gpu(dev);
172 case PS3_MATCH_ID_AV_SETTINGS:
173 case PS3_MATCH_ID_SYSTEM_MANAGER:
174 pr_debug("%s:%d: unsupported match_id: %u\n", __func__,
175 __LINE__, dev->match_id);
176 pr_debug("%s:%d: bus_id: %llu\n", __func__, __LINE__,
185 pr_debug("%s:%d: unknown match_id: %u\n", __func__, __LINE__,
190 EXPORT_SYMBOL_GPL(ps3_open_hv_device);
192 int ps3_close_hv_device(struct ps3_system_bus_device *dev)
195 pr_debug("%s:%d: match_id: %u\n", __func__, __LINE__, dev->match_id);
197 switch (dev->match_id) {
198 case PS3_MATCH_ID_EHCI:
199 case PS3_MATCH_ID_OHCI:
200 case PS3_MATCH_ID_GELIC:
201 case PS3_MATCH_ID_STOR_DISK:
202 case PS3_MATCH_ID_STOR_ROM:
203 case PS3_MATCH_ID_STOR_FLASH:
204 return ps3_close_hv_device_sb(dev);
206 case PS3_MATCH_ID_SOUND:
207 case PS3_MATCH_ID_GPU:
208 return ps3_close_hv_device_gpu(dev);
210 case PS3_MATCH_ID_AV_SETTINGS:
211 case PS3_MATCH_ID_SYSTEM_MANAGER:
212 pr_debug("%s:%d: unsupported match_id: %u\n", __func__,
213 __LINE__, dev->match_id);
214 pr_debug("%s:%d: bus_id: %llu\n", __func__, __LINE__,
223 pr_debug("%s:%d: unknown match_id: %u\n", __func__, __LINE__,
228 EXPORT_SYMBOL_GPL(ps3_close_hv_device);
230 #define dump_mmio_region(_a) _dump_mmio_region(_a, __func__, __LINE__)
231 static void _dump_mmio_region(const struct ps3_mmio_region* r,
232 const char* func, int line)
234 pr_debug("%s:%d: dev %llu:%llu\n", func, line, r->dev->bus_id,
236 pr_debug("%s:%d: bus_addr %lxh\n", func, line, r->bus_addr);
237 pr_debug("%s:%d: len %lxh\n", func, line, r->len);
238 pr_debug("%s:%d: lpar_addr %lxh\n", func, line, r->lpar_addr);
241 static int ps3_sb_mmio_region_create(struct ps3_mmio_region *r)
246 result = lv1_map_device_mmio_region(r->dev->bus_id, r->dev->dev_id,
247 r->bus_addr, r->len, r->page_size, &lpar_addr);
248 r->lpar_addr = lpar_addr;
251 pr_debug("%s:%d: lv1_map_device_mmio_region failed: %s\n",
252 __func__, __LINE__, ps3_result(result));
260 static int ps3_ioc0_mmio_region_create(struct ps3_mmio_region *r)
262 /* device specific; do nothing currently */
266 int ps3_mmio_region_create(struct ps3_mmio_region *r)
268 return r->mmio_ops->create(r);
270 EXPORT_SYMBOL_GPL(ps3_mmio_region_create);
272 static int ps3_sb_free_mmio_region(struct ps3_mmio_region *r)
277 result = lv1_unmap_device_mmio_region(r->dev->bus_id, r->dev->dev_id,
281 pr_debug("%s:%d: lv1_unmap_device_mmio_region failed: %s\n",
282 __func__, __LINE__, ps3_result(result));
288 static int ps3_ioc0_free_mmio_region(struct ps3_mmio_region *r)
290 /* device specific; do nothing currently */
295 int ps3_free_mmio_region(struct ps3_mmio_region *r)
297 return r->mmio_ops->free(r);
300 EXPORT_SYMBOL_GPL(ps3_free_mmio_region);
302 static const struct ps3_mmio_region_ops ps3_mmio_sb_region_ops = {
303 .create = ps3_sb_mmio_region_create,
304 .free = ps3_sb_free_mmio_region
307 static const struct ps3_mmio_region_ops ps3_mmio_ioc0_region_ops = {
308 .create = ps3_ioc0_mmio_region_create,
309 .free = ps3_ioc0_free_mmio_region
312 int ps3_mmio_region_init(struct ps3_system_bus_device *dev,
313 struct ps3_mmio_region *r, unsigned long bus_addr, unsigned long len,
314 enum ps3_mmio_page_size page_size)
317 r->bus_addr = bus_addr;
319 r->page_size = page_size;
320 switch (dev->dev_type) {
321 case PS3_DEVICE_TYPE_SB:
322 r->mmio_ops = &ps3_mmio_sb_region_ops;
324 case PS3_DEVICE_TYPE_IOC0:
325 r->mmio_ops = &ps3_mmio_ioc0_region_ops;
333 EXPORT_SYMBOL_GPL(ps3_mmio_region_init);
335 static int ps3_system_bus_match(struct device *_dev,
336 const struct device_driver *_drv)
339 const struct ps3_system_bus_driver *drv = ps3_drv_to_system_bus_drv(_drv);
340 struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
342 if (!dev->match_sub_id)
343 result = dev->match_id == drv->match_id;
345 result = dev->match_sub_id == drv->match_sub_id &&
346 dev->match_id == drv->match_id;
349 pr_info("%s:%d: dev=%u.%u(%s), drv=%u.%u(%s): match\n",
351 dev->match_id, dev->match_sub_id, dev_name(&dev->core),
352 drv->match_id, drv->match_sub_id, drv->core.name);
354 pr_debug("%s:%d: dev=%u.%u(%s), drv=%u.%u(%s): miss\n",
356 dev->match_id, dev->match_sub_id, dev_name(&dev->core),
357 drv->match_id, drv->match_sub_id, drv->core.name);
362 static int ps3_system_bus_probe(struct device *_dev)
365 struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
366 struct ps3_system_bus_driver *drv;
369 dev_dbg(_dev, "%s:%d\n", __func__, __LINE__);
371 drv = ps3_system_bus_dev_to_system_bus_drv(dev);
375 result = drv->probe(dev);
377 pr_debug("%s:%d: %s no probe method\n", __func__, __LINE__,
378 dev_name(&dev->core));
380 pr_debug(" <- %s:%d: %s\n", __func__, __LINE__, dev_name(&dev->core));
384 static void ps3_system_bus_remove(struct device *_dev)
386 struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
387 struct ps3_system_bus_driver *drv;
390 dev_dbg(_dev, "%s:%d\n", __func__, __LINE__);
392 drv = ps3_system_bus_dev_to_system_bus_drv(dev);
398 dev_dbg(&dev->core, "%s:%d %s: no remove method\n",
399 __func__, __LINE__, drv->core.name);
401 pr_debug(" <- %s:%d: %s\n", __func__, __LINE__, dev_name(&dev->core));
404 static void ps3_system_bus_shutdown(struct device *_dev)
406 struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
407 struct ps3_system_bus_driver *drv;
411 dev_dbg(&dev->core, " -> %s:%d: match_id %d\n", __func__, __LINE__,
414 if (!dev->core.driver) {
415 dev_dbg(&dev->core, "%s:%d: no driver bound\n", __func__,
420 drv = ps3_system_bus_dev_to_system_bus_drv(dev);
424 dev_dbg(&dev->core, "%s:%d: %s -> %s\n", __func__, __LINE__,
425 dev_name(&dev->core), drv->core.name);
429 else if (drv->remove) {
430 dev_dbg(&dev->core, "%s:%d %s: no shutdown, calling remove\n",
431 __func__, __LINE__, drv->core.name);
434 dev_dbg(&dev->core, "%s:%d %s: no shutdown method\n",
435 __func__, __LINE__, drv->core.name);
439 dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
442 static int ps3_system_bus_uevent(const struct device *_dev, struct kobj_uevent_env *env)
444 struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
446 if (add_uevent_var(env, "MODALIAS=ps3:%d:%d", dev->match_id,
452 static ssize_t modalias_show(struct device *_dev, struct device_attribute *a,
455 struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
457 return sysfs_emit(buf, "ps3:%d:%d\n", dev->match_id,
460 static DEVICE_ATTR_RO(modalias);
462 static struct attribute *ps3_system_bus_dev_attrs[] = {
463 &dev_attr_modalias.attr,
466 ATTRIBUTE_GROUPS(ps3_system_bus_dev);
468 static struct bus_type ps3_system_bus_type = {
469 .name = "ps3_system_bus",
470 .match = ps3_system_bus_match,
471 .uevent = ps3_system_bus_uevent,
472 .probe = ps3_system_bus_probe,
473 .remove = ps3_system_bus_remove,
474 .shutdown = ps3_system_bus_shutdown,
475 .dev_groups = ps3_system_bus_dev_groups,
478 static int __init ps3_system_bus_init(void)
482 if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
485 pr_debug(" -> %s:%d\n", __func__, __LINE__);
487 mutex_init(&usage_hack.mutex);
489 result = device_register(&ps3_system_bus);
492 result = bus_register(&ps3_system_bus_type);
495 pr_debug(" <- %s:%d\n", __func__, __LINE__);
499 core_initcall(ps3_system_bus_init);
501 /* Allocates a contiguous real buffer and creates mappings over it.
502 * Returns the virtual address of the buffer and sets dma_handle
503 * to the dma address (mapping) of the first page.
505 static void * ps3_alloc_coherent(struct device *_dev, size_t size,
506 dma_addr_t *dma_handle, gfp_t flag,
510 struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
511 unsigned long virt_addr;
513 flag &= ~(__GFP_DMA | __GFP_HIGHMEM);
516 virt_addr = __get_free_pages(flag, get_order(size));
519 pr_debug("%s:%d: get_free_pages failed\n", __func__, __LINE__);
523 result = ps3_dma_map(dev->d_region, virt_addr, size, dma_handle,
524 CBE_IOPTE_PP_W | CBE_IOPTE_PP_R |
525 CBE_IOPTE_SO_RW | CBE_IOPTE_M);
528 pr_debug("%s:%d: ps3_dma_map failed (%d)\n",
529 __func__, __LINE__, result);
530 BUG_ON("check region type");
534 return (void*)virt_addr;
537 free_pages(virt_addr, get_order(size));
543 static void ps3_free_coherent(struct device *_dev, size_t size, void *vaddr,
544 dma_addr_t dma_handle, unsigned long attrs)
546 struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
548 ps3_dma_unmap(dev->d_region, dma_handle, size);
549 free_pages((unsigned long)vaddr, get_order(size));
552 /* Creates TCEs for a user provided buffer. The user buffer must be
553 * contiguous real kernel storage (not vmalloc). The address passed here
554 * comprises a page address and offset into that page. The dma_addr_t
555 * returned will point to the same byte within the page as was passed in.
558 static dma_addr_t ps3_sb_map_page(struct device *_dev, struct page *page,
559 unsigned long offset, size_t size, enum dma_data_direction direction,
562 struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
565 void *ptr = page_address(page) + offset;
567 result = ps3_dma_map(dev->d_region, (unsigned long)ptr, size,
569 CBE_IOPTE_PP_R | CBE_IOPTE_PP_W |
570 CBE_IOPTE_SO_RW | CBE_IOPTE_M);
573 pr_debug("%s:%d: ps3_dma_map failed (%d)\n",
574 __func__, __LINE__, result);
580 static dma_addr_t ps3_ioc0_map_page(struct device *_dev, struct page *page,
581 unsigned long offset, size_t size,
582 enum dma_data_direction direction,
585 struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
589 void *ptr = page_address(page) + offset;
591 iopte_flag = CBE_IOPTE_M;
593 case DMA_BIDIRECTIONAL:
594 iopte_flag |= CBE_IOPTE_PP_R | CBE_IOPTE_PP_W | CBE_IOPTE_SO_RW;
597 iopte_flag |= CBE_IOPTE_PP_R | CBE_IOPTE_SO_R;
599 case DMA_FROM_DEVICE:
600 iopte_flag |= CBE_IOPTE_PP_W | CBE_IOPTE_SO_RW;
606 result = ps3_dma_map(dev->d_region, (unsigned long)ptr, size,
607 &bus_addr, iopte_flag);
610 pr_debug("%s:%d: ps3_dma_map failed (%d)\n",
611 __func__, __LINE__, result);
616 static void ps3_unmap_page(struct device *_dev, dma_addr_t dma_addr,
617 size_t size, enum dma_data_direction direction, unsigned long attrs)
619 struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
622 result = ps3_dma_unmap(dev->d_region, dma_addr, size);
625 pr_debug("%s:%d: ps3_dma_unmap failed (%d)\n",
626 __func__, __LINE__, result);
630 static int ps3_sb_map_sg(struct device *_dev, struct scatterlist *sgl,
631 int nents, enum dma_data_direction direction, unsigned long attrs)
633 #if defined(CONFIG_PS3_DYNAMIC_DMA)
637 struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
638 struct scatterlist *sg;
641 for_each_sg(sgl, sg, nents, i) {
642 int result = ps3_dma_map(dev->d_region, sg_phys(sg),
643 sg->length, &sg->dma_address, 0);
646 pr_debug("%s:%d: ps3_dma_map failed (%d)\n",
647 __func__, __LINE__, result);
651 sg->dma_length = sg->length;
658 static int ps3_ioc0_map_sg(struct device *_dev, struct scatterlist *sg,
660 enum dma_data_direction direction,
667 static void ps3_sb_unmap_sg(struct device *_dev, struct scatterlist *sg,
668 int nents, enum dma_data_direction direction, unsigned long attrs)
670 #if defined(CONFIG_PS3_DYNAMIC_DMA)
675 static void ps3_ioc0_unmap_sg(struct device *_dev, struct scatterlist *sg,
676 int nents, enum dma_data_direction direction,
682 static int ps3_dma_supported(struct device *_dev, u64 mask)
684 return mask >= DMA_BIT_MASK(32);
687 static const struct dma_map_ops ps3_sb_dma_ops = {
688 .alloc = ps3_alloc_coherent,
689 .free = ps3_free_coherent,
690 .map_sg = ps3_sb_map_sg,
691 .unmap_sg = ps3_sb_unmap_sg,
692 .dma_supported = ps3_dma_supported,
693 .map_page = ps3_sb_map_page,
694 .unmap_page = ps3_unmap_page,
695 .mmap = dma_common_mmap,
696 .get_sgtable = dma_common_get_sgtable,
697 .alloc_pages_op = dma_common_alloc_pages,
698 .free_pages = dma_common_free_pages,
701 static const struct dma_map_ops ps3_ioc0_dma_ops = {
702 .alloc = ps3_alloc_coherent,
703 .free = ps3_free_coherent,
704 .map_sg = ps3_ioc0_map_sg,
705 .unmap_sg = ps3_ioc0_unmap_sg,
706 .dma_supported = ps3_dma_supported,
707 .map_page = ps3_ioc0_map_page,
708 .unmap_page = ps3_unmap_page,
709 .mmap = dma_common_mmap,
710 .get_sgtable = dma_common_get_sgtable,
711 .alloc_pages_op = dma_common_alloc_pages,
712 .free_pages = dma_common_free_pages,
716 * ps3_system_bus_release_device - remove a device from the system bus
719 static void ps3_system_bus_release_device(struct device *_dev)
721 struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
726 * ps3_system_bus_device_register - add a device to the system bus
728 * ps3_system_bus_device_register() expects the dev object to be allocated
729 * dynamically by the caller. The system bus takes ownership of the dev
730 * object and frees the object in ps3_system_bus_release_device().
733 int ps3_system_bus_device_register(struct ps3_system_bus_device *dev)
736 static unsigned int dev_ioc0_count;
737 static unsigned int dev_sb_count;
738 static unsigned int dev_vuart_count;
739 static unsigned int dev_lpm_count;
741 if (!dev->core.parent)
742 dev->core.parent = &ps3_system_bus;
743 dev->core.bus = &ps3_system_bus_type;
744 dev->core.release = ps3_system_bus_release_device;
746 switch (dev->dev_type) {
747 case PS3_DEVICE_TYPE_IOC0:
748 dev->core.dma_ops = &ps3_ioc0_dma_ops;
749 dev_set_name(&dev->core, "ioc0_%02x", ++dev_ioc0_count);
751 case PS3_DEVICE_TYPE_SB:
752 dev->core.dma_ops = &ps3_sb_dma_ops;
753 dev_set_name(&dev->core, "sb_%02x", ++dev_sb_count);
756 case PS3_DEVICE_TYPE_VUART:
757 dev_set_name(&dev->core, "vuart_%02x", ++dev_vuart_count);
759 case PS3_DEVICE_TYPE_LPM:
760 dev_set_name(&dev->core, "lpm_%02x", ++dev_lpm_count);
766 dev->core.of_node = NULL;
767 set_dev_node(&dev->core, 0);
769 pr_debug("%s:%d add %s\n", __func__, __LINE__, dev_name(&dev->core));
771 result = device_register(&dev->core);
775 EXPORT_SYMBOL_GPL(ps3_system_bus_device_register);
777 int ps3_system_bus_driver_register(struct ps3_system_bus_driver *drv)
781 pr_debug(" -> %s:%d: %s\n", __func__, __LINE__, drv->core.name);
783 if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
786 drv->core.bus = &ps3_system_bus_type;
788 result = driver_register(&drv->core);
789 pr_debug(" <- %s:%d: %s\n", __func__, __LINE__, drv->core.name);
793 EXPORT_SYMBOL_GPL(ps3_system_bus_driver_register);
795 void ps3_system_bus_driver_unregister(struct ps3_system_bus_driver *drv)
797 pr_debug(" -> %s:%d: %s\n", __func__, __LINE__, drv->core.name);
798 driver_unregister(&drv->core);
799 pr_debug(" <- %s:%d: %s\n", __func__, __LINE__, drv->core.name);
802 EXPORT_SYMBOL_GPL(ps3_system_bus_driver_unregister);