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-mapping.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_debug("%s:%d: lv1_open_device failed: %s\n", __func__,
68 __LINE__, ps3_result(result));
73 mutex_unlock(&usage_hack.mutex);
77 static int ps3_close_hv_device_sb(struct ps3_system_bus_device *dev)
82 mutex_lock(&usage_hack.mutex);
84 if (ps3_is_device(dev, 1, 1)) {
86 if (usage_hack.sb_11) {
92 if (ps3_is_device(dev, 1, 2)) {
94 if (usage_hack.sb_12) {
100 result = lv1_close_device(dev->bus_id, dev->dev_id);
104 mutex_unlock(&usage_hack.mutex);
108 static int ps3_open_hv_device_gpu(struct ps3_system_bus_device *dev)
112 mutex_lock(&usage_hack.mutex);
115 if (usage_hack.gpu > 1) {
120 result = lv1_gpu_open(0);
123 pr_debug("%s:%d: lv1_gpu_open failed: %s\n", __func__,
124 __LINE__, ps3_result(result));
129 mutex_unlock(&usage_hack.mutex);
133 static int ps3_close_hv_device_gpu(struct ps3_system_bus_device *dev)
137 mutex_lock(&usage_hack.mutex);
140 if (usage_hack.gpu) {
145 result = lv1_gpu_close();
149 mutex_unlock(&usage_hack.mutex);
153 int ps3_open_hv_device(struct ps3_system_bus_device *dev)
156 pr_debug("%s:%d: match_id: %u\n", __func__, __LINE__, dev->match_id);
158 switch (dev->match_id) {
159 case PS3_MATCH_ID_EHCI:
160 case PS3_MATCH_ID_OHCI:
161 case PS3_MATCH_ID_GELIC:
162 case PS3_MATCH_ID_STOR_DISK:
163 case PS3_MATCH_ID_STOR_ROM:
164 case PS3_MATCH_ID_STOR_FLASH:
165 return ps3_open_hv_device_sb(dev);
167 case PS3_MATCH_ID_SOUND:
168 case PS3_MATCH_ID_GPU:
169 return ps3_open_hv_device_gpu(dev);
171 case PS3_MATCH_ID_AV_SETTINGS:
172 case PS3_MATCH_ID_SYSTEM_MANAGER:
173 pr_debug("%s:%d: unsupported match_id: %u\n", __func__,
174 __LINE__, dev->match_id);
175 pr_debug("%s:%d: bus_id: %llu\n", __func__, __LINE__,
184 pr_debug("%s:%d: unknown match_id: %u\n", __func__, __LINE__,
189 EXPORT_SYMBOL_GPL(ps3_open_hv_device);
191 int ps3_close_hv_device(struct ps3_system_bus_device *dev)
194 pr_debug("%s:%d: match_id: %u\n", __func__, __LINE__, dev->match_id);
196 switch (dev->match_id) {
197 case PS3_MATCH_ID_EHCI:
198 case PS3_MATCH_ID_OHCI:
199 case PS3_MATCH_ID_GELIC:
200 case PS3_MATCH_ID_STOR_DISK:
201 case PS3_MATCH_ID_STOR_ROM:
202 case PS3_MATCH_ID_STOR_FLASH:
203 return ps3_close_hv_device_sb(dev);
205 case PS3_MATCH_ID_SOUND:
206 case PS3_MATCH_ID_GPU:
207 return ps3_close_hv_device_gpu(dev);
209 case PS3_MATCH_ID_AV_SETTINGS:
210 case PS3_MATCH_ID_SYSTEM_MANAGER:
211 pr_debug("%s:%d: unsupported match_id: %u\n", __func__,
212 __LINE__, dev->match_id);
213 pr_debug("%s:%d: bus_id: %llu\n", __func__, __LINE__,
222 pr_debug("%s:%d: unknown match_id: %u\n", __func__, __LINE__,
227 EXPORT_SYMBOL_GPL(ps3_close_hv_device);
229 #define dump_mmio_region(_a) _dump_mmio_region(_a, __func__, __LINE__)
230 static void _dump_mmio_region(const struct ps3_mmio_region* r,
231 const char* func, int line)
233 pr_debug("%s:%d: dev %llu:%llu\n", func, line, r->dev->bus_id,
235 pr_debug("%s:%d: bus_addr %lxh\n", func, line, r->bus_addr);
236 pr_debug("%s:%d: len %lxh\n", func, line, r->len);
237 pr_debug("%s:%d: lpar_addr %lxh\n", func, line, r->lpar_addr);
240 static int ps3_sb_mmio_region_create(struct ps3_mmio_region *r)
245 result = lv1_map_device_mmio_region(r->dev->bus_id, r->dev->dev_id,
246 r->bus_addr, r->len, r->page_size, &lpar_addr);
247 r->lpar_addr = lpar_addr;
250 pr_debug("%s:%d: lv1_map_device_mmio_region failed: %s\n",
251 __func__, __LINE__, ps3_result(result));
259 static int ps3_ioc0_mmio_region_create(struct ps3_mmio_region *r)
261 /* device specific; do nothing currently */
265 int ps3_mmio_region_create(struct ps3_mmio_region *r)
267 return r->mmio_ops->create(r);
269 EXPORT_SYMBOL_GPL(ps3_mmio_region_create);
271 static int ps3_sb_free_mmio_region(struct ps3_mmio_region *r)
276 result = lv1_unmap_device_mmio_region(r->dev->bus_id, r->dev->dev_id,
280 pr_debug("%s:%d: lv1_unmap_device_mmio_region failed: %s\n",
281 __func__, __LINE__, ps3_result(result));
287 static int ps3_ioc0_free_mmio_region(struct ps3_mmio_region *r)
289 /* device specific; do nothing currently */
294 int ps3_free_mmio_region(struct ps3_mmio_region *r)
296 return r->mmio_ops->free(r);
299 EXPORT_SYMBOL_GPL(ps3_free_mmio_region);
301 static const struct ps3_mmio_region_ops ps3_mmio_sb_region_ops = {
302 .create = ps3_sb_mmio_region_create,
303 .free = ps3_sb_free_mmio_region
306 static const struct ps3_mmio_region_ops ps3_mmio_ioc0_region_ops = {
307 .create = ps3_ioc0_mmio_region_create,
308 .free = ps3_ioc0_free_mmio_region
311 int ps3_mmio_region_init(struct ps3_system_bus_device *dev,
312 struct ps3_mmio_region *r, unsigned long bus_addr, unsigned long len,
313 enum ps3_mmio_page_size page_size)
316 r->bus_addr = bus_addr;
318 r->page_size = page_size;
319 switch (dev->dev_type) {
320 case PS3_DEVICE_TYPE_SB:
321 r->mmio_ops = &ps3_mmio_sb_region_ops;
323 case PS3_DEVICE_TYPE_IOC0:
324 r->mmio_ops = &ps3_mmio_ioc0_region_ops;
332 EXPORT_SYMBOL_GPL(ps3_mmio_region_init);
334 static int ps3_system_bus_match(struct device *_dev,
335 struct device_driver *_drv)
338 struct ps3_system_bus_driver *drv = ps3_drv_to_system_bus_drv(_drv);
339 struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
341 if (!dev->match_sub_id)
342 result = dev->match_id == drv->match_id;
344 result = dev->match_sub_id == drv->match_sub_id &&
345 dev->match_id == drv->match_id;
348 pr_info("%s:%d: dev=%u.%u(%s), drv=%u.%u(%s): match\n",
350 dev->match_id, dev->match_sub_id, dev_name(&dev->core),
351 drv->match_id, drv->match_sub_id, drv->core.name);
353 pr_debug("%s:%d: dev=%u.%u(%s), drv=%u.%u(%s): miss\n",
355 dev->match_id, dev->match_sub_id, dev_name(&dev->core),
356 drv->match_id, drv->match_sub_id, drv->core.name);
361 static int ps3_system_bus_probe(struct device *_dev)
364 struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
365 struct ps3_system_bus_driver *drv;
368 dev_dbg(_dev, "%s:%d\n", __func__, __LINE__);
370 drv = ps3_system_bus_dev_to_system_bus_drv(dev);
374 result = drv->probe(dev);
376 pr_debug("%s:%d: %s no probe method\n", __func__, __LINE__,
377 dev_name(&dev->core));
379 pr_debug(" <- %s:%d: %s\n", __func__, __LINE__, dev_name(&dev->core));
383 static int 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);
396 result = drv->remove(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));
405 static void ps3_system_bus_shutdown(struct device *_dev)
407 struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
408 struct ps3_system_bus_driver *drv;
412 dev_dbg(&dev->core, " -> %s:%d: match_id %d\n", __func__, __LINE__,
415 if (!dev->core.driver) {
416 dev_dbg(&dev->core, "%s:%d: no driver bound\n", __func__,
421 drv = ps3_system_bus_dev_to_system_bus_drv(dev);
425 dev_dbg(&dev->core, "%s:%d: %s -> %s\n", __func__, __LINE__,
426 dev_name(&dev->core), drv->core.name);
430 else if (drv->remove) {
431 dev_dbg(&dev->core, "%s:%d %s: no shutdown, calling remove\n",
432 __func__, __LINE__, drv->core.name);
435 dev_dbg(&dev->core, "%s:%d %s: no shutdown method\n",
436 __func__, __LINE__, drv->core.name);
440 dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
443 static int ps3_system_bus_uevent(struct device *_dev, struct kobj_uevent_env *env)
445 struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
447 if (add_uevent_var(env, "MODALIAS=ps3:%d:%d", dev->match_id,
453 static ssize_t modalias_show(struct device *_dev, struct device_attribute *a,
456 struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
457 int len = snprintf(buf, PAGE_SIZE, "ps3:%d:%d\n", dev->match_id,
460 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
462 static DEVICE_ATTR_RO(modalias);
464 static struct attribute *ps3_system_bus_dev_attrs[] = {
465 &dev_attr_modalias.attr,
468 ATTRIBUTE_GROUPS(ps3_system_bus_dev);
470 struct bus_type ps3_system_bus_type = {
471 .name = "ps3_system_bus",
472 .match = ps3_system_bus_match,
473 .uevent = ps3_system_bus_uevent,
474 .probe = ps3_system_bus_probe,
475 .remove = ps3_system_bus_remove,
476 .shutdown = ps3_system_bus_shutdown,
477 .dev_groups = ps3_system_bus_dev_groups,
480 static int __init ps3_system_bus_init(void)
484 if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
487 pr_debug(" -> %s:%d\n", __func__, __LINE__);
489 mutex_init(&usage_hack.mutex);
491 result = device_register(&ps3_system_bus);
494 result = bus_register(&ps3_system_bus_type);
497 pr_debug(" <- %s:%d\n", __func__, __LINE__);
501 core_initcall(ps3_system_bus_init);
503 /* Allocates a contiguous real buffer and creates mappings over it.
504 * Returns the virtual address of the buffer and sets dma_handle
505 * to the dma address (mapping) of the first page.
507 static void * ps3_alloc_coherent(struct device *_dev, size_t size,
508 dma_addr_t *dma_handle, gfp_t flag,
512 struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
513 unsigned long virt_addr;
515 flag &= ~(__GFP_DMA | __GFP_HIGHMEM);
518 virt_addr = __get_free_pages(flag, get_order(size));
521 pr_debug("%s:%d: get_free_pages failed\n", __func__, __LINE__);
525 result = ps3_dma_map(dev->d_region, virt_addr, size, dma_handle,
526 CBE_IOPTE_PP_W | CBE_IOPTE_PP_R |
527 CBE_IOPTE_SO_RW | CBE_IOPTE_M);
530 pr_debug("%s:%d: ps3_dma_map failed (%d)\n",
531 __func__, __LINE__, result);
532 BUG_ON("check region type");
536 return (void*)virt_addr;
539 free_pages(virt_addr, get_order(size));
545 static void ps3_free_coherent(struct device *_dev, size_t size, void *vaddr,
546 dma_addr_t dma_handle, unsigned long attrs)
548 struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
550 ps3_dma_unmap(dev->d_region, dma_handle, size);
551 free_pages((unsigned long)vaddr, get_order(size));
554 /* Creates TCEs for a user provided buffer. The user buffer must be
555 * contiguous real kernel storage (not vmalloc). The address passed here
556 * comprises a page address and offset into that page. The dma_addr_t
557 * returned will point to the same byte within the page as was passed in.
560 static dma_addr_t ps3_sb_map_page(struct device *_dev, struct page *page,
561 unsigned long offset, size_t size, enum dma_data_direction direction,
564 struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
567 void *ptr = page_address(page) + offset;
569 result = ps3_dma_map(dev->d_region, (unsigned long)ptr, size,
571 CBE_IOPTE_PP_R | CBE_IOPTE_PP_W |
572 CBE_IOPTE_SO_RW | CBE_IOPTE_M);
575 pr_debug("%s:%d: ps3_dma_map failed (%d)\n",
576 __func__, __LINE__, result);
582 static dma_addr_t ps3_ioc0_map_page(struct device *_dev, struct page *page,
583 unsigned long offset, size_t size,
584 enum dma_data_direction direction,
587 struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
591 void *ptr = page_address(page) + offset;
593 iopte_flag = CBE_IOPTE_M;
595 case DMA_BIDIRECTIONAL:
596 iopte_flag |= CBE_IOPTE_PP_R | CBE_IOPTE_PP_W | CBE_IOPTE_SO_RW;
599 iopte_flag |= CBE_IOPTE_PP_R | CBE_IOPTE_SO_R;
601 case DMA_FROM_DEVICE:
602 iopte_flag |= CBE_IOPTE_PP_W | CBE_IOPTE_SO_RW;
608 result = ps3_dma_map(dev->d_region, (unsigned long)ptr, size,
609 &bus_addr, iopte_flag);
612 pr_debug("%s:%d: ps3_dma_map failed (%d)\n",
613 __func__, __LINE__, result);
618 static void ps3_unmap_page(struct device *_dev, dma_addr_t dma_addr,
619 size_t size, enum dma_data_direction direction, unsigned long attrs)
621 struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
624 result = ps3_dma_unmap(dev->d_region, dma_addr, size);
627 pr_debug("%s:%d: ps3_dma_unmap failed (%d)\n",
628 __func__, __LINE__, result);
632 static int ps3_sb_map_sg(struct device *_dev, struct scatterlist *sgl,
633 int nents, enum dma_data_direction direction, unsigned long attrs)
635 #if defined(CONFIG_PS3_DYNAMIC_DMA)
639 struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
640 struct scatterlist *sg;
643 for_each_sg(sgl, sg, nents, i) {
644 int result = ps3_dma_map(dev->d_region, sg_phys(sg),
645 sg->length, &sg->dma_address, 0);
648 pr_debug("%s:%d: ps3_dma_map failed (%d)\n",
649 __func__, __LINE__, result);
653 sg->dma_length = sg->length;
660 static int ps3_ioc0_map_sg(struct device *_dev, struct scatterlist *sg,
662 enum dma_data_direction direction,
669 static void ps3_sb_unmap_sg(struct device *_dev, struct scatterlist *sg,
670 int nents, enum dma_data_direction direction, unsigned long attrs)
672 #if defined(CONFIG_PS3_DYNAMIC_DMA)
677 static void ps3_ioc0_unmap_sg(struct device *_dev, struct scatterlist *sg,
678 int nents, enum dma_data_direction direction,
684 static int ps3_dma_supported(struct device *_dev, u64 mask)
686 return mask >= DMA_BIT_MASK(32);
689 static const struct dma_map_ops ps3_sb_dma_ops = {
690 .alloc = ps3_alloc_coherent,
691 .free = ps3_free_coherent,
692 .map_sg = ps3_sb_map_sg,
693 .unmap_sg = ps3_sb_unmap_sg,
694 .dma_supported = ps3_dma_supported,
695 .map_page = ps3_sb_map_page,
696 .unmap_page = ps3_unmap_page,
697 .mmap = dma_common_mmap,
698 .get_sgtable = dma_common_get_sgtable,
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,
714 * ps3_system_bus_release_device - remove a device from the system bus
717 static void ps3_system_bus_release_device(struct device *_dev)
719 struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
724 * ps3_system_bus_device_register - add a device to the system bus
726 * ps3_system_bus_device_register() expects the dev object to be allocated
727 * dynamically by the caller. The system bus takes ownership of the dev
728 * object and frees the object in ps3_system_bus_release_device().
731 int ps3_system_bus_device_register(struct ps3_system_bus_device *dev)
734 static unsigned int dev_ioc0_count;
735 static unsigned int dev_sb_count;
736 static unsigned int dev_vuart_count;
737 static unsigned int dev_lpm_count;
739 if (!dev->core.parent)
740 dev->core.parent = &ps3_system_bus;
741 dev->core.bus = &ps3_system_bus_type;
742 dev->core.release = ps3_system_bus_release_device;
744 switch (dev->dev_type) {
745 case PS3_DEVICE_TYPE_IOC0:
746 dev->core.dma_ops = &ps3_ioc0_dma_ops;
747 dev_set_name(&dev->core, "ioc0_%02x", ++dev_ioc0_count);
749 case PS3_DEVICE_TYPE_SB:
750 dev->core.dma_ops = &ps3_sb_dma_ops;
751 dev_set_name(&dev->core, "sb_%02x", ++dev_sb_count);
754 case PS3_DEVICE_TYPE_VUART:
755 dev_set_name(&dev->core, "vuart_%02x", ++dev_vuart_count);
757 case PS3_DEVICE_TYPE_LPM:
758 dev_set_name(&dev->core, "lpm_%02x", ++dev_lpm_count);
764 dev->core.of_node = NULL;
765 set_dev_node(&dev->core, 0);
767 pr_debug("%s:%d add %s\n", __func__, __LINE__, dev_name(&dev->core));
769 result = device_register(&dev->core);
773 EXPORT_SYMBOL_GPL(ps3_system_bus_device_register);
775 int ps3_system_bus_driver_register(struct ps3_system_bus_driver *drv)
779 pr_debug(" -> %s:%d: %s\n", __func__, __LINE__, drv->core.name);
781 if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
784 drv->core.bus = &ps3_system_bus_type;
786 result = driver_register(&drv->core);
787 pr_debug(" <- %s:%d: %s\n", __func__, __LINE__, drv->core.name);
791 EXPORT_SYMBOL_GPL(ps3_system_bus_driver_register);
793 void ps3_system_bus_driver_unregister(struct ps3_system_bus_driver *drv)
795 pr_debug(" -> %s:%d: %s\n", __func__, __LINE__, drv->core.name);
796 driver_unregister(&drv->core);
797 pr_debug(" <- %s:%d: %s\n", __func__, __LINE__, drv->core.name);
800 EXPORT_SYMBOL_GPL(ps3_system_bus_driver_unregister);