1 // SPDX-License-Identifier: GPL-2.0-only
3 * PS3 device registration routines.
5 * Copyright (C) 2007 Sony Computer Entertainment Inc.
6 * Copyright 2007 Sony Corp.
9 #include <linux/delay.h>
10 #include <linux/freezer.h>
11 #include <linux/kernel.h>
12 #include <linux/kthread.h>
13 #include <linux/init.h>
14 #include <linux/slab.h>
15 #include <linux/reboot.h>
17 #include <asm/firmware.h>
18 #include <asm/lv1call.h>
19 #include <asm/ps3stor.h>
23 static int __init ps3_register_lpm_devices(void)
28 struct ps3_system_bus_device *dev;
30 pr_debug(" -> %s:%d\n", __func__, __LINE__);
32 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
36 dev->match_id = PS3_MATCH_ID_LPM;
37 dev->dev_type = PS3_DEVICE_TYPE_LPM;
39 /* The current lpm driver only supports a single BE processor. */
41 result = ps3_repository_read_be_node_id(0, &dev->lpm.node_id);
44 pr_debug("%s:%d: ps3_repository_read_be_node_id failed \n",
49 result = ps3_repository_read_lpm_privileges(dev->lpm.node_id, &tmp1,
53 pr_debug("%s:%d: ps3_repository_read_lpm_privileges failed\n",
58 lv1_get_logical_partition_id(&tmp2);
61 pr_debug("%s:%d: wrong lpar\n",
67 if (!(dev->lpm.rights & PS3_LPM_RIGHTS_USE_LPM)) {
68 pr_debug("%s:%d: don't have rights to use lpm\n",
74 pr_debug("%s:%d: pu_id %llu, rights %llu(%llxh)\n",
75 __func__, __LINE__, dev->lpm.pu_id, dev->lpm.rights,
78 result = ps3_repository_read_pu_id(0, &dev->lpm.pu_id);
81 pr_debug("%s:%d: ps3_repository_read_pu_id failed \n",
86 result = ps3_system_bus_device_register(dev);
89 pr_debug("%s:%d ps3_system_bus_device_register failed\n",
94 pr_debug(" <- %s:%d\n", __func__, __LINE__);
102 pr_debug(" <- %s:%d: failed\n", __func__, __LINE__);
107 * ps3_setup_gelic_device - Setup and register a gelic device instance.
109 * Allocates memory for a struct ps3_system_bus_device instance, initialises the
110 * structure members, and registers the device instance with the system bus.
113 static int __init ps3_setup_gelic_device(
114 const struct ps3_repository_device *repo)
118 struct ps3_system_bus_device dev;
119 struct ps3_dma_region d_region;
122 pr_debug(" -> %s:%d\n", __func__, __LINE__);
124 BUG_ON(repo->bus_type != PS3_BUS_TYPE_SB);
125 BUG_ON(repo->dev_type != PS3_DEV_TYPE_SB_GELIC);
127 p = kzalloc(sizeof(struct layout), GFP_KERNEL);
134 p->dev.match_id = PS3_MATCH_ID_GELIC;
135 p->dev.dev_type = PS3_DEVICE_TYPE_SB;
136 p->dev.bus_id = repo->bus_id;
137 p->dev.dev_id = repo->dev_id;
138 p->dev.d_region = &p->d_region;
140 result = ps3_repository_find_interrupt(repo,
141 PS3_INTERRUPT_TYPE_EVENT_PORT, &p->dev.interrupt_id);
144 pr_debug("%s:%d ps3_repository_find_interrupt failed\n",
146 goto fail_find_interrupt;
149 BUG_ON(p->dev.interrupt_id != 0);
151 result = ps3_dma_region_init(&p->dev, p->dev.d_region, PS3_DMA_64K,
152 PS3_DMA_OTHER, NULL, 0);
155 pr_debug("%s:%d ps3_dma_region_init failed\n",
160 result = ps3_system_bus_device_register(&p->dev);
163 pr_debug("%s:%d ps3_system_bus_device_register failed\n",
165 goto fail_device_register;
168 pr_debug(" <- %s:%d\n", __func__, __LINE__);
171 fail_device_register:
176 pr_debug(" <- %s:%d: fail.\n", __func__, __LINE__);
180 static int __ref ps3_setup_uhc_device(
181 const struct ps3_repository_device *repo, enum ps3_match_id match_id,
182 enum ps3_interrupt_type interrupt_type, enum ps3_reg_type reg_type)
186 struct ps3_system_bus_device dev;
187 struct ps3_dma_region d_region;
188 struct ps3_mmio_region m_region;
193 pr_debug(" -> %s:%d\n", __func__, __LINE__);
195 BUG_ON(repo->bus_type != PS3_BUS_TYPE_SB);
196 BUG_ON(repo->dev_type != PS3_DEV_TYPE_SB_USB);
198 p = kzalloc(sizeof(struct layout), GFP_KERNEL);
205 p->dev.match_id = match_id;
206 p->dev.dev_type = PS3_DEVICE_TYPE_SB;
207 p->dev.bus_id = repo->bus_id;
208 p->dev.dev_id = repo->dev_id;
209 p->dev.d_region = &p->d_region;
210 p->dev.m_region = &p->m_region;
212 result = ps3_repository_find_interrupt(repo,
213 interrupt_type, &p->dev.interrupt_id);
216 pr_debug("%s:%d ps3_repository_find_interrupt failed\n",
218 goto fail_find_interrupt;
221 result = ps3_repository_find_reg(repo, reg_type,
225 pr_debug("%s:%d ps3_repository_find_reg failed\n",
230 result = ps3_dma_region_init(&p->dev, p->dev.d_region, PS3_DMA_64K,
231 PS3_DMA_INTERNAL, NULL, 0);
234 pr_debug("%s:%d ps3_dma_region_init failed\n",
239 result = ps3_mmio_region_init(&p->dev, p->dev.m_region, bus_addr, len,
243 pr_debug("%s:%d ps3_mmio_region_init failed\n",
248 result = ps3_system_bus_device_register(&p->dev);
251 pr_debug("%s:%d ps3_system_bus_device_register failed\n",
253 goto fail_device_register;
256 pr_debug(" <- %s:%d\n", __func__, __LINE__);
259 fail_device_register:
266 pr_debug(" <- %s:%d: fail.\n", __func__, __LINE__);
270 static int __init ps3_setup_ehci_device(
271 const struct ps3_repository_device *repo)
273 return ps3_setup_uhc_device(repo, PS3_MATCH_ID_EHCI,
274 PS3_INTERRUPT_TYPE_SB_EHCI, PS3_REG_TYPE_SB_EHCI);
277 static int __init ps3_setup_ohci_device(
278 const struct ps3_repository_device *repo)
280 return ps3_setup_uhc_device(repo, PS3_MATCH_ID_OHCI,
281 PS3_INTERRUPT_TYPE_SB_OHCI, PS3_REG_TYPE_SB_OHCI);
284 static int __init ps3_setup_vuart_device(enum ps3_match_id match_id,
285 unsigned int port_number)
289 struct ps3_system_bus_device dev;
292 pr_debug(" -> %s:%d: match_id %u, port %u\n", __func__, __LINE__,
293 match_id, port_number);
295 p = kzalloc(sizeof(struct layout), GFP_KERNEL);
300 p->dev.match_id = match_id;
301 p->dev.dev_type = PS3_DEVICE_TYPE_VUART;
302 p->dev.port_number = port_number;
304 result = ps3_system_bus_device_register(&p->dev);
307 pr_debug("%s:%d ps3_system_bus_device_register failed\n",
309 goto fail_device_register;
311 pr_debug(" <- %s:%d\n", __func__, __LINE__);
314 fail_device_register:
316 pr_debug(" <- %s:%d fail\n", __func__, __LINE__);
320 static int ps3_setup_storage_dev(const struct ps3_repository_device *repo,
321 enum ps3_match_id match_id)
324 struct ps3_storage_device *p;
325 u64 port, blk_size, num_blocks;
326 unsigned int num_regions, i;
328 pr_debug(" -> %s:%u: match_id %u\n", __func__, __LINE__, match_id);
330 result = ps3_repository_read_stor_dev_info(repo->bus_index,
331 repo->dev_index, &port,
332 &blk_size, &num_blocks,
335 printk(KERN_ERR "%s:%u: _read_stor_dev_info failed %d\n",
336 __func__, __LINE__, result);
340 pr_debug("%s:%u: (%u:%u:%u): port %llu blk_size %llu num_blocks %llu "
341 "num_regions %u\n", __func__, __LINE__, repo->bus_index,
342 repo->dev_index, repo->dev_type, port, blk_size, num_blocks,
345 p = kzalloc(struct_size(p, regions, num_regions), GFP_KERNEL);
351 p->sbd.match_id = match_id;
352 p->sbd.dev_type = PS3_DEVICE_TYPE_SB;
353 p->sbd.bus_id = repo->bus_id;
354 p->sbd.dev_id = repo->dev_id;
355 p->sbd.d_region = &p->dma_region;
356 p->blk_size = blk_size;
357 p->num_regions = num_regions;
359 result = ps3_repository_find_interrupt(repo,
360 PS3_INTERRUPT_TYPE_EVENT_PORT,
361 &p->sbd.interrupt_id);
363 printk(KERN_ERR "%s:%u: find_interrupt failed %d\n", __func__,
366 goto fail_find_interrupt;
369 for (i = 0; i < num_regions; i++) {
373 result = ps3_repository_read_stor_dev_region(repo->bus_index,
379 "%s:%u: read_stor_dev_region failed %d\n",
380 __func__, __LINE__, result);
382 goto fail_read_region;
384 pr_debug("%s:%u: region %u: id %u start %llu size %llu\n",
385 __func__, __LINE__, i, id, start, size);
387 p->regions[i].id = id;
388 p->regions[i].start = start;
389 p->regions[i].size = size;
392 result = ps3_system_bus_device_register(&p->sbd);
394 pr_debug("%s:%u ps3_system_bus_device_register failed\n",
396 goto fail_device_register;
399 pr_debug(" <- %s:%u\n", __func__, __LINE__);
402 fail_device_register:
407 pr_debug(" <- %s:%u: fail.\n", __func__, __LINE__);
411 static int __init ps3_register_vuart_devices(void)
414 unsigned int port_number;
416 pr_debug(" -> %s:%d\n", __func__, __LINE__);
418 result = ps3_repository_read_vuart_av_port(&port_number);
420 port_number = 0; /* av default */
422 result = ps3_setup_vuart_device(PS3_MATCH_ID_AV_SETTINGS, port_number);
425 result = ps3_repository_read_vuart_sysmgr_port(&port_number);
427 port_number = 2; /* sysmgr default */
429 result = ps3_setup_vuart_device(PS3_MATCH_ID_SYSTEM_MANAGER,
433 pr_debug(" <- %s:%d\n", __func__, __LINE__);
437 static int __init ps3_register_sound_devices(void)
441 struct ps3_system_bus_device dev;
442 struct ps3_dma_region d_region;
443 struct ps3_mmio_region m_region;
446 pr_debug(" -> %s:%d\n", __func__, __LINE__);
448 p = kzalloc(sizeof(*p), GFP_KERNEL);
452 p->dev.match_id = PS3_MATCH_ID_SOUND;
453 p->dev.dev_type = PS3_DEVICE_TYPE_IOC0;
454 p->dev.d_region = &p->d_region;
455 p->dev.m_region = &p->m_region;
457 result = ps3_system_bus_device_register(&p->dev);
460 pr_debug("%s:%d ps3_system_bus_device_register failed\n",
462 goto fail_device_register;
464 pr_debug(" <- %s:%d\n", __func__, __LINE__);
467 fail_device_register:
469 pr_debug(" <- %s:%d failed\n", __func__, __LINE__);
473 static int __init ps3_register_graphics_devices(void)
477 struct ps3_system_bus_device dev;
480 pr_debug(" -> %s:%d\n", __func__, __LINE__);
482 p = kzalloc(sizeof(struct layout), GFP_KERNEL);
487 p->dev.match_id = PS3_MATCH_ID_GPU;
488 p->dev.match_sub_id = PS3_MATCH_SUB_ID_GPU_FB;
489 p->dev.dev_type = PS3_DEVICE_TYPE_IOC0;
491 result = ps3_system_bus_device_register(&p->dev);
494 pr_debug("%s:%d ps3_system_bus_device_register failed\n",
496 goto fail_device_register;
499 pr_debug(" <- %s:%d\n", __func__, __LINE__);
502 fail_device_register:
504 pr_debug(" <- %s:%d failed\n", __func__, __LINE__);
508 static int __init ps3_register_ramdisk_device(void)
512 struct ps3_system_bus_device dev;
515 pr_debug(" -> %s:%d\n", __func__, __LINE__);
517 p = kzalloc(sizeof(struct layout), GFP_KERNEL);
522 p->dev.match_id = PS3_MATCH_ID_GPU;
523 p->dev.match_sub_id = PS3_MATCH_SUB_ID_GPU_RAMDISK;
524 p->dev.dev_type = PS3_DEVICE_TYPE_IOC0;
526 result = ps3_system_bus_device_register(&p->dev);
529 pr_debug("%s:%d ps3_system_bus_device_register failed\n",
531 goto fail_device_register;
534 pr_debug(" <- %s:%d\n", __func__, __LINE__);
537 fail_device_register:
539 pr_debug(" <- %s:%d failed\n", __func__, __LINE__);
544 * ps3_setup_dynamic_device - Setup a dynamic device from the repository
547 static int ps3_setup_dynamic_device(const struct ps3_repository_device *repo)
551 switch (repo->dev_type) {
552 case PS3_DEV_TYPE_STOR_DISK:
553 result = ps3_setup_storage_dev(repo, PS3_MATCH_ID_STOR_DISK);
555 /* Some devices are not accessible from the Other OS lpar. */
556 if (result == -ENODEV) {
558 pr_debug("%s:%u: not accessible\n", __func__,
563 pr_debug("%s:%u ps3_setup_storage_dev failed\n",
567 case PS3_DEV_TYPE_STOR_ROM:
568 result = ps3_setup_storage_dev(repo, PS3_MATCH_ID_STOR_ROM);
570 pr_debug("%s:%u ps3_setup_storage_dev failed\n",
574 case PS3_DEV_TYPE_STOR_FLASH:
575 result = ps3_setup_storage_dev(repo, PS3_MATCH_ID_STOR_FLASH);
577 pr_debug("%s:%u ps3_setup_storage_dev failed\n",
583 pr_debug("%s:%u: unsupported dev_type %u\n", __func__, __LINE__,
591 * ps3_setup_static_device - Setup a static device from the repository
594 static int __init ps3_setup_static_device(const struct ps3_repository_device *repo)
598 switch (repo->dev_type) {
599 case PS3_DEV_TYPE_SB_GELIC:
600 result = ps3_setup_gelic_device(repo);
602 pr_debug("%s:%d ps3_setup_gelic_device failed\n",
606 case PS3_DEV_TYPE_SB_USB:
608 /* Each USB device has both an EHCI and an OHCI HC */
610 result = ps3_setup_ehci_device(repo);
613 pr_debug("%s:%d ps3_setup_ehci_device failed\n",
617 result = ps3_setup_ohci_device(repo);
620 pr_debug("%s:%d ps3_setup_ohci_device failed\n",
626 return ps3_setup_dynamic_device(repo);
632 static void ps3_find_and_add_device(u64 bus_id, u64 dev_id)
634 struct ps3_repository_device repo;
636 unsigned int retries;
640 * On some firmware versions (e.g. 1.90), the device may not show up
641 * in the repository immediately
643 for (retries = 0; retries < 10; retries++) {
644 res = ps3_repository_find_device_by_id(&repo, bus_id, dev_id);
648 rem = msleep_interruptible(100);
652 pr_warn("%s:%u: device %llu:%llu not found\n",
653 __func__, __LINE__, bus_id, dev_id);
658 pr_debug("%s:%u: device %llu:%llu found after %u retries\n",
659 __func__, __LINE__, bus_id, dev_id, retries);
661 ps3_setup_dynamic_device(&repo);
665 #define PS3_NOTIFICATION_DEV_ID ULONG_MAX
666 #define PS3_NOTIFICATION_INTERRUPT_ID 0
668 struct ps3_notification_device {
669 struct ps3_system_bus_device sbd;
673 struct completion done;
676 enum ps3_notify_type {
677 notify_device_ready = 0,
678 notify_region_probe = 1,
679 notify_region_update = 2,
682 struct ps3_notify_cmd {
683 u64 operation_code; /* must be zero */
684 u64 event_mask; /* OR of 1UL << enum ps3_notify_type */
687 struct ps3_notify_event {
688 u64 event_type; /* enum ps3_notify_type */
695 static irqreturn_t ps3_notification_interrupt(int irq, void *data)
697 struct ps3_notification_device *dev = data;
701 spin_lock(&dev->lock);
702 res = lv1_storage_get_async_status(PS3_NOTIFICATION_DEV_ID, &tag,
705 pr_err("%s:%u: tag mismatch, got %llx, expected %llx\n",
706 __func__, __LINE__, tag, dev->tag);
709 pr_err("%s:%u: res %d status 0x%llx\n", __func__, __LINE__, res,
712 pr_debug("%s:%u: completed, status 0x%llx\n", __func__,
714 dev->lv1_status = status;
715 complete(&dev->done);
717 spin_unlock(&dev->lock);
721 static int ps3_notification_read_write(struct ps3_notification_device *dev,
724 const char *op = write ? "write" : "read";
728 init_completion(&dev->done);
729 spin_lock_irqsave(&dev->lock, flags);
730 res = write ? lv1_storage_write(dev->sbd.dev_id, 0, 0, 1, 0, lpar,
732 : lv1_storage_read(dev->sbd.dev_id, 0, 0, 1, 0, lpar,
734 spin_unlock_irqrestore(&dev->lock, flags);
736 pr_err("%s:%u: %s failed %d\n", __func__, __LINE__, op, res);
739 pr_debug("%s:%u: notification %s issued\n", __func__, __LINE__, op);
741 res = wait_event_interruptible(dev->done.wait,
742 dev->done.done || kthread_should_stop());
743 if (kthread_should_stop())
746 pr_debug("%s:%u: interrupted %s\n", __func__, __LINE__, op);
750 if (dev->lv1_status) {
751 pr_err("%s:%u: %s not completed, status 0x%llx\n", __func__,
752 __LINE__, op, dev->lv1_status);
755 pr_debug("%s:%u: notification %s completed\n", __func__, __LINE__, op);
760 static struct task_struct *probe_task;
763 * ps3_probe_thread - Background repository probing at system startup.
765 * This implementation only supports background probing on a single bus.
766 * It uses the hypervisor's storage device notification mechanism to wait until
767 * a storage device is ready. The device notification mechanism uses a
768 * pseudo device to asynchronously notify the guest when storage devices become
769 * ready. The notification device has a block size of 512 bytes.
772 static int ps3_probe_thread(void *data)
774 struct ps3_notification_device dev;
779 struct ps3_notify_cmd *notify_cmd;
780 struct ps3_notify_event *notify_event;
782 pr_debug(" -> %s:%u: kthread started\n", __func__, __LINE__);
784 buf = kzalloc(512, GFP_KERNEL);
788 lpar = ps3_mm_phys_to_lpar(__pa(buf));
792 /* dummy system bus device */
793 dev.sbd.bus_id = (u64)data;
794 dev.sbd.dev_id = PS3_NOTIFICATION_DEV_ID;
795 dev.sbd.interrupt_id = PS3_NOTIFICATION_INTERRUPT_ID;
797 res = lv1_open_device(dev.sbd.bus_id, dev.sbd.dev_id, 0);
799 pr_err("%s:%u: lv1_open_device failed %s\n", __func__,
800 __LINE__, ps3_result(res));
804 res = ps3_sb_event_receive_port_setup(&dev.sbd, PS3_BINDING_CPU_ANY,
807 pr_err("%s:%u: ps3_sb_event_receive_port_setup failed %d\n",
808 __func__, __LINE__, res);
809 goto fail_close_device;
812 spin_lock_init(&dev.lock);
814 res = request_irq(irq, ps3_notification_interrupt, 0,
815 "ps3_notification", &dev);
817 pr_err("%s:%u: request_irq failed %d\n", __func__, __LINE__,
819 goto fail_sb_event_receive_port_destroy;
822 /* Setup and write the request for device notification. */
823 notify_cmd->operation_code = 0; /* must be zero */
824 notify_cmd->event_mask = 1UL << notify_region_probe;
826 res = ps3_notification_read_write(&dev, lpar, 1);
830 /* Loop here processing the requested notification events. */
834 memset(notify_event, 0, sizeof(*notify_event));
836 res = ps3_notification_read_write(&dev, lpar, 0);
840 pr_debug("%s:%u: notify event type 0x%llx bus id %llu dev id %llu"
841 " type %llu port %llu\n", __func__, __LINE__,
842 notify_event->event_type, notify_event->bus_id,
843 notify_event->dev_id, notify_event->dev_type,
844 notify_event->dev_port);
846 if (notify_event->event_type != notify_region_probe ||
847 notify_event->bus_id != dev.sbd.bus_id) {
848 pr_warn("%s:%u: bad notify_event: event %llu, dev_id %llu, dev_type %llu\n",
849 __func__, __LINE__, notify_event->event_type,
850 notify_event->dev_id, notify_event->dev_type);
854 ps3_find_and_add_device(dev.sbd.bus_id, notify_event->dev_id);
856 } while (!kthread_should_stop());
860 fail_sb_event_receive_port_destroy:
861 ps3_sb_event_receive_port_destroy(&dev.sbd, irq);
863 lv1_close_device(dev.sbd.bus_id, dev.sbd.dev_id);
869 pr_debug(" <- %s:%u: kthread finished\n", __func__, __LINE__);
875 * ps3_stop_probe_thread - Stops the background probe thread.
879 static int ps3_stop_probe_thread(struct notifier_block *nb, unsigned long code,
883 kthread_stop(probe_task);
887 static struct notifier_block nb = {
888 .notifier_call = ps3_stop_probe_thread
892 * ps3_start_probe_thread - Starts the background probe thread.
896 static int __init ps3_start_probe_thread(enum ps3_bus_type bus_type)
899 struct task_struct *task;
900 struct ps3_repository_device repo;
902 pr_debug(" -> %s:%d\n", __func__, __LINE__);
904 memset(&repo, 0, sizeof(repo));
906 repo.bus_type = bus_type;
908 result = ps3_repository_find_bus(repo.bus_type, 0, &repo.bus_index);
911 printk(KERN_ERR "%s: Cannot find bus (%d)\n", __func__, result);
915 result = ps3_repository_read_bus_id(repo.bus_index, &repo.bus_id);
918 printk(KERN_ERR "%s: read_bus_id failed %d\n", __func__,
923 task = kthread_run(ps3_probe_thread, (void *)repo.bus_id,
924 "ps3-probe-%u", bus_type);
927 result = PTR_ERR(task);
928 printk(KERN_ERR "%s: kthread_run failed %d\n", __func__,
934 register_reboot_notifier(&nb);
936 pr_debug(" <- %s:%d\n", __func__, __LINE__);
941 * ps3_register_devices - Probe the system and register devices found.
943 * A device_initcall() routine.
946 static int __init ps3_register_devices(void)
950 if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
953 pr_debug(" -> %s:%d\n", __func__, __LINE__);
955 /* ps3_repository_dump_bus_info(); */
957 result = ps3_start_probe_thread(PS3_BUS_TYPE_STORAGE);
959 ps3_register_vuart_devices();
961 ps3_register_graphics_devices();
963 ps3_repository_find_devices(PS3_BUS_TYPE_SB, ps3_setup_static_device);
965 ps3_register_sound_devices();
967 ps3_register_lpm_devices();
969 ps3_register_ramdisk_device();
971 pr_debug(" <- %s:%d\n", __func__, __LINE__);
975 device_initcall(ps3_register_devices);