2 * PS3 device registration routines.
4 * Copyright (C) 2007 Sony Computer Entertainment Inc.
5 * Copyright 2007 Sony Corp.
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.
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.
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
21 #include <linux/delay.h>
22 #include <linux/freezer.h>
23 #include <linux/kernel.h>
24 #include <linux/kthread.h>
25 #include <linux/init.h>
26 #include <linux/slab.h>
27 #include <linux/reboot.h>
29 #include <asm/firmware.h>
30 #include <asm/lv1call.h>
31 #include <asm/ps3stor.h>
35 static int __init ps3_register_lpm_devices(void)
40 struct ps3_system_bus_device *dev;
42 pr_debug(" -> %s:%d\n", __func__, __LINE__);
44 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
48 dev->match_id = PS3_MATCH_ID_LPM;
49 dev->dev_type = PS3_DEVICE_TYPE_LPM;
51 /* The current lpm driver only supports a single BE processor. */
53 result = ps3_repository_read_be_node_id(0, &dev->lpm.node_id);
56 pr_debug("%s:%d: ps3_repository_read_be_node_id failed \n",
61 result = ps3_repository_read_lpm_privileges(dev->lpm.node_id, &tmp1,
65 pr_debug("%s:%d: ps3_repository_read_lpm_privleges failed \n",
70 lv1_get_logical_partition_id(&tmp2);
73 pr_debug("%s:%d: wrong lpar\n",
79 if (!(dev->lpm.rights & PS3_LPM_RIGHTS_USE_LPM)) {
80 pr_debug("%s:%d: don't have rights to use lpm\n",
86 pr_debug("%s:%d: pu_id %llu, rights %llu(%llxh)\n",
87 __func__, __LINE__, dev->lpm.pu_id, dev->lpm.rights,
90 result = ps3_repository_read_pu_id(0, &dev->lpm.pu_id);
93 pr_debug("%s:%d: ps3_repository_read_pu_id failed \n",
98 result = ps3_system_bus_device_register(dev);
101 pr_debug("%s:%d ps3_system_bus_device_register failed\n",
106 pr_debug(" <- %s:%d\n", __func__, __LINE__);
114 pr_debug(" <- %s:%d: failed\n", __func__, __LINE__);
119 * ps3_setup_gelic_device - Setup and register a gelic device instance.
121 * Allocates memory for a struct ps3_system_bus_device instance, initialises the
122 * structure members, and registers the device instance with the system bus.
125 static int __init ps3_setup_gelic_device(
126 const struct ps3_repository_device *repo)
130 struct ps3_system_bus_device dev;
131 struct ps3_dma_region d_region;
134 pr_debug(" -> %s:%d\n", __func__, __LINE__);
136 BUG_ON(repo->bus_type != PS3_BUS_TYPE_SB);
137 BUG_ON(repo->dev_type != PS3_DEV_TYPE_SB_GELIC);
139 p = kzalloc(sizeof(struct layout), GFP_KERNEL);
146 p->dev.match_id = PS3_MATCH_ID_GELIC;
147 p->dev.dev_type = PS3_DEVICE_TYPE_SB;
148 p->dev.bus_id = repo->bus_id;
149 p->dev.dev_id = repo->dev_id;
150 p->dev.d_region = &p->d_region;
152 result = ps3_repository_find_interrupt(repo,
153 PS3_INTERRUPT_TYPE_EVENT_PORT, &p->dev.interrupt_id);
156 pr_debug("%s:%d ps3_repository_find_interrupt failed\n",
158 goto fail_find_interrupt;
161 BUG_ON(p->dev.interrupt_id != 0);
163 result = ps3_dma_region_init(&p->dev, p->dev.d_region, PS3_DMA_64K,
164 PS3_DMA_OTHER, NULL, 0);
167 pr_debug("%s:%d ps3_dma_region_init failed\n",
172 result = ps3_system_bus_device_register(&p->dev);
175 pr_debug("%s:%d ps3_system_bus_device_register failed\n",
177 goto fail_device_register;
180 pr_debug(" <- %s:%d\n", __func__, __LINE__);
183 fail_device_register:
188 pr_debug(" <- %s:%d: fail.\n", __func__, __LINE__);
192 static int __init_refok ps3_setup_uhc_device(
193 const struct ps3_repository_device *repo, enum ps3_match_id match_id,
194 enum ps3_interrupt_type interrupt_type, enum ps3_reg_type reg_type)
198 struct ps3_system_bus_device dev;
199 struct ps3_dma_region d_region;
200 struct ps3_mmio_region m_region;
205 pr_debug(" -> %s:%d\n", __func__, __LINE__);
207 BUG_ON(repo->bus_type != PS3_BUS_TYPE_SB);
208 BUG_ON(repo->dev_type != PS3_DEV_TYPE_SB_USB);
210 p = kzalloc(sizeof(struct layout), GFP_KERNEL);
217 p->dev.match_id = match_id;
218 p->dev.dev_type = PS3_DEVICE_TYPE_SB;
219 p->dev.bus_id = repo->bus_id;
220 p->dev.dev_id = repo->dev_id;
221 p->dev.d_region = &p->d_region;
222 p->dev.m_region = &p->m_region;
224 result = ps3_repository_find_interrupt(repo,
225 interrupt_type, &p->dev.interrupt_id);
228 pr_debug("%s:%d ps3_repository_find_interrupt failed\n",
230 goto fail_find_interrupt;
233 result = ps3_repository_find_reg(repo, reg_type,
237 pr_debug("%s:%d ps3_repository_find_reg failed\n",
242 result = ps3_dma_region_init(&p->dev, p->dev.d_region, PS3_DMA_64K,
243 PS3_DMA_INTERNAL, NULL, 0);
246 pr_debug("%s:%d ps3_dma_region_init failed\n",
251 result = ps3_mmio_region_init(&p->dev, p->dev.m_region, bus_addr, len,
255 pr_debug("%s:%d ps3_mmio_region_init failed\n",
260 result = ps3_system_bus_device_register(&p->dev);
263 pr_debug("%s:%d ps3_system_bus_device_register failed\n",
265 goto fail_device_register;
268 pr_debug(" <- %s:%d\n", __func__, __LINE__);
271 fail_device_register:
278 pr_debug(" <- %s:%d: fail.\n", __func__, __LINE__);
282 static int __init ps3_setup_ehci_device(
283 const struct ps3_repository_device *repo)
285 return ps3_setup_uhc_device(repo, PS3_MATCH_ID_EHCI,
286 PS3_INTERRUPT_TYPE_SB_EHCI, PS3_REG_TYPE_SB_EHCI);
289 static int __init ps3_setup_ohci_device(
290 const struct ps3_repository_device *repo)
292 return ps3_setup_uhc_device(repo, PS3_MATCH_ID_OHCI,
293 PS3_INTERRUPT_TYPE_SB_OHCI, PS3_REG_TYPE_SB_OHCI);
296 static int __init ps3_setup_vuart_device(enum ps3_match_id match_id,
297 unsigned int port_number)
301 struct ps3_system_bus_device dev;
304 pr_debug(" -> %s:%d: match_id %u, port %u\n", __func__, __LINE__,
305 match_id, port_number);
307 p = kzalloc(sizeof(struct layout), GFP_KERNEL);
312 p->dev.match_id = match_id;
313 p->dev.dev_type = PS3_DEVICE_TYPE_VUART;
314 p->dev.port_number = port_number;
316 result = ps3_system_bus_device_register(&p->dev);
319 pr_debug("%s:%d ps3_system_bus_device_register failed\n",
321 goto fail_device_register;
323 pr_debug(" <- %s:%d\n", __func__, __LINE__);
326 fail_device_register:
328 pr_debug(" <- %s:%d fail\n", __func__, __LINE__);
332 static int ps3_setup_storage_dev(const struct ps3_repository_device *repo,
333 enum ps3_match_id match_id)
336 struct ps3_storage_device *p;
337 u64 port, blk_size, num_blocks;
338 unsigned int num_regions, i;
340 pr_debug(" -> %s:%u: match_id %u\n", __func__, __LINE__, match_id);
342 result = ps3_repository_read_stor_dev_info(repo->bus_index,
343 repo->dev_index, &port,
344 &blk_size, &num_blocks,
347 printk(KERN_ERR "%s:%u: _read_stor_dev_info failed %d\n",
348 __func__, __LINE__, result);
352 pr_debug("%s:%u: (%u:%u:%u): port %llu blk_size %llu num_blocks %llu "
353 "num_regions %u\n", __func__, __LINE__, repo->bus_index,
354 repo->dev_index, repo->dev_type, port, blk_size, num_blocks,
357 p = kzalloc(sizeof(struct ps3_storage_device) +
358 num_regions * sizeof(struct ps3_storage_region),
365 p->sbd.match_id = match_id;
366 p->sbd.dev_type = PS3_DEVICE_TYPE_SB;
367 p->sbd.bus_id = repo->bus_id;
368 p->sbd.dev_id = repo->dev_id;
369 p->sbd.d_region = &p->dma_region;
370 p->blk_size = blk_size;
371 p->num_regions = num_regions;
373 result = ps3_repository_find_interrupt(repo,
374 PS3_INTERRUPT_TYPE_EVENT_PORT,
375 &p->sbd.interrupt_id);
377 printk(KERN_ERR "%s:%u: find_interrupt failed %d\n", __func__,
380 goto fail_find_interrupt;
383 for (i = 0; i < num_regions; i++) {
387 result = ps3_repository_read_stor_dev_region(repo->bus_index,
393 "%s:%u: read_stor_dev_region failed %d\n",
394 __func__, __LINE__, result);
396 goto fail_read_region;
398 pr_debug("%s:%u: region %u: id %u start %llu size %llu\n",
399 __func__, __LINE__, i, id, start, size);
401 p->regions[i].id = id;
402 p->regions[i].start = start;
403 p->regions[i].size = size;
406 result = ps3_system_bus_device_register(&p->sbd);
408 pr_debug("%s:%u ps3_system_bus_device_register failed\n",
410 goto fail_device_register;
413 pr_debug(" <- %s:%u\n", __func__, __LINE__);
416 fail_device_register:
421 pr_debug(" <- %s:%u: fail.\n", __func__, __LINE__);
425 static int __init ps3_register_vuart_devices(void)
428 unsigned int port_number;
430 pr_debug(" -> %s:%d\n", __func__, __LINE__);
432 result = ps3_repository_read_vuart_av_port(&port_number);
434 port_number = 0; /* av default */
436 result = ps3_setup_vuart_device(PS3_MATCH_ID_AV_SETTINGS, port_number);
439 result = ps3_repository_read_vuart_sysmgr_port(&port_number);
441 port_number = 2; /* sysmgr default */
443 result = ps3_setup_vuart_device(PS3_MATCH_ID_SYSTEM_MANAGER,
447 pr_debug(" <- %s:%d\n", __func__, __LINE__);
451 static int __init ps3_register_sound_devices(void)
455 struct ps3_system_bus_device dev;
456 struct ps3_dma_region d_region;
457 struct ps3_mmio_region m_region;
460 pr_debug(" -> %s:%d\n", __func__, __LINE__);
462 p = kzalloc(sizeof(*p), GFP_KERNEL);
466 p->dev.match_id = PS3_MATCH_ID_SOUND;
467 p->dev.dev_type = PS3_DEVICE_TYPE_IOC0;
468 p->dev.d_region = &p->d_region;
469 p->dev.m_region = &p->m_region;
471 result = ps3_system_bus_device_register(&p->dev);
474 pr_debug("%s:%d ps3_system_bus_device_register failed\n",
476 goto fail_device_register;
478 pr_debug(" <- %s:%d\n", __func__, __LINE__);
481 fail_device_register:
483 pr_debug(" <- %s:%d failed\n", __func__, __LINE__);
487 static int __init ps3_register_graphics_devices(void)
491 struct ps3_system_bus_device dev;
494 pr_debug(" -> %s:%d\n", __func__, __LINE__);
496 p = kzalloc(sizeof(struct layout), GFP_KERNEL);
501 p->dev.match_id = PS3_MATCH_ID_GPU;
502 p->dev.match_sub_id = PS3_MATCH_SUB_ID_GPU_FB;
503 p->dev.dev_type = PS3_DEVICE_TYPE_IOC0;
505 result = ps3_system_bus_device_register(&p->dev);
508 pr_debug("%s:%d ps3_system_bus_device_register failed\n",
510 goto fail_device_register;
513 pr_debug(" <- %s:%d\n", __func__, __LINE__);
516 fail_device_register:
518 pr_debug(" <- %s:%d failed\n", __func__, __LINE__);
522 static int __init ps3_register_ramdisk_device(void)
526 struct ps3_system_bus_device dev;
529 pr_debug(" -> %s:%d\n", __func__, __LINE__);
531 p = kzalloc(sizeof(struct layout), GFP_KERNEL);
536 p->dev.match_id = PS3_MATCH_ID_GPU;
537 p->dev.match_sub_id = PS3_MATCH_SUB_ID_GPU_RAMDISK;
538 p->dev.dev_type = PS3_DEVICE_TYPE_IOC0;
540 result = ps3_system_bus_device_register(&p->dev);
543 pr_debug("%s:%d ps3_system_bus_device_register failed\n",
545 goto fail_device_register;
548 pr_debug(" <- %s:%d\n", __func__, __LINE__);
551 fail_device_register:
553 pr_debug(" <- %s:%d failed\n", __func__, __LINE__);
558 * ps3_setup_dynamic_device - Setup a dynamic device from the repository
561 static int ps3_setup_dynamic_device(const struct ps3_repository_device *repo)
565 switch (repo->dev_type) {
566 case PS3_DEV_TYPE_STOR_DISK:
567 result = ps3_setup_storage_dev(repo, PS3_MATCH_ID_STOR_DISK);
569 /* Some devices are not accessable from the Other OS lpar. */
570 if (result == -ENODEV) {
572 pr_debug("%s:%u: not accessable\n", __func__,
577 pr_debug("%s:%u ps3_setup_storage_dev failed\n",
581 case PS3_DEV_TYPE_STOR_ROM:
582 result = ps3_setup_storage_dev(repo, PS3_MATCH_ID_STOR_ROM);
584 pr_debug("%s:%u ps3_setup_storage_dev failed\n",
588 case PS3_DEV_TYPE_STOR_FLASH:
589 result = ps3_setup_storage_dev(repo, PS3_MATCH_ID_STOR_FLASH);
591 pr_debug("%s:%u ps3_setup_storage_dev failed\n",
597 pr_debug("%s:%u: unsupported dev_type %u\n", __func__, __LINE__,
605 * ps3_setup_static_device - Setup a static device from the repository
608 static int __init ps3_setup_static_device(const struct ps3_repository_device *repo)
612 switch (repo->dev_type) {
613 case PS3_DEV_TYPE_SB_GELIC:
614 result = ps3_setup_gelic_device(repo);
616 pr_debug("%s:%d ps3_setup_gelic_device failed\n",
620 case PS3_DEV_TYPE_SB_USB:
622 /* Each USB device has both an EHCI and an OHCI HC */
624 result = ps3_setup_ehci_device(repo);
627 pr_debug("%s:%d ps3_setup_ehci_device failed\n",
631 result = ps3_setup_ohci_device(repo);
634 pr_debug("%s:%d ps3_setup_ohci_device failed\n",
640 return ps3_setup_dynamic_device(repo);
646 static void ps3_find_and_add_device(u64 bus_id, u64 dev_id)
648 struct ps3_repository_device repo;
650 unsigned int retries;
654 * On some firmware versions (e.g. 1.90), the device may not show up
655 * in the repository immediately
657 for (retries = 0; retries < 10; retries++) {
658 res = ps3_repository_find_device_by_id(&repo, bus_id, dev_id);
662 rem = msleep_interruptible(100);
666 pr_warning("%s:%u: device %llu:%llu not found\n", __func__, __LINE__,
672 pr_debug("%s:%u: device %llu:%llu found after %u retries\n",
673 __func__, __LINE__, bus_id, dev_id, retries);
675 ps3_setup_dynamic_device(&repo);
679 #define PS3_NOTIFICATION_DEV_ID ULONG_MAX
680 #define PS3_NOTIFICATION_INTERRUPT_ID 0
682 struct ps3_notification_device {
683 struct ps3_system_bus_device sbd;
687 struct completion done;
690 enum ps3_notify_type {
691 notify_device_ready = 0,
692 notify_region_probe = 1,
693 notify_region_update = 2,
696 struct ps3_notify_cmd {
697 u64 operation_code; /* must be zero */
698 u64 event_mask; /* OR of 1UL << enum ps3_notify_type */
701 struct ps3_notify_event {
702 u64 event_type; /* enum ps3_notify_type */
709 static irqreturn_t ps3_notification_interrupt(int irq, void *data)
711 struct ps3_notification_device *dev = data;
715 spin_lock(&dev->lock);
716 res = lv1_storage_get_async_status(PS3_NOTIFICATION_DEV_ID, &tag,
719 pr_err("%s:%u: tag mismatch, got %llx, expected %llx\n",
720 __func__, __LINE__, tag, dev->tag);
723 pr_err("%s:%u: res %d status 0x%llx\n", __func__, __LINE__, res,
726 pr_debug("%s:%u: completed, status 0x%llx\n", __func__,
728 dev->lv1_status = status;
729 complete(&dev->done);
731 spin_unlock(&dev->lock);
735 static int ps3_notification_read_write(struct ps3_notification_device *dev,
738 const char *op = write ? "write" : "read";
742 init_completion(&dev->done);
743 spin_lock_irqsave(&dev->lock, flags);
744 res = write ? lv1_storage_write(dev->sbd.dev_id, 0, 0, 1, 0, lpar,
746 : lv1_storage_read(dev->sbd.dev_id, 0, 0, 1, 0, lpar,
748 spin_unlock_irqrestore(&dev->lock, flags);
750 pr_err("%s:%u: %s failed %d\n", __func__, __LINE__, op, res);
753 pr_debug("%s:%u: notification %s issued\n", __func__, __LINE__, op);
755 res = wait_event_interruptible(dev->done.wait,
756 dev->done.done || kthread_should_stop());
757 if (kthread_should_stop())
760 pr_debug("%s:%u: interrupted %s\n", __func__, __LINE__, op);
764 if (dev->lv1_status) {
765 pr_err("%s:%u: %s not completed, status 0x%llx\n", __func__,
766 __LINE__, op, dev->lv1_status);
769 pr_debug("%s:%u: notification %s completed\n", __func__, __LINE__, op);
774 static struct task_struct *probe_task;
777 * ps3_probe_thread - Background repository probing at system startup.
779 * This implementation only supports background probing on a single bus.
780 * It uses the hypervisor's storage device notification mechanism to wait until
781 * a storage device is ready. The device notification mechanism uses a
782 * pseudo device to asynchronously notify the guest when storage devices become
783 * ready. The notification device has a block size of 512 bytes.
786 static int ps3_probe_thread(void *data)
788 struct ps3_notification_device dev;
793 struct ps3_notify_cmd *notify_cmd;
794 struct ps3_notify_event *notify_event;
796 pr_debug(" -> %s:%u: kthread started\n", __func__, __LINE__);
798 buf = kzalloc(512, GFP_KERNEL);
802 lpar = ps3_mm_phys_to_lpar(__pa(buf));
806 /* dummy system bus device */
807 dev.sbd.bus_id = (u64)data;
808 dev.sbd.dev_id = PS3_NOTIFICATION_DEV_ID;
809 dev.sbd.interrupt_id = PS3_NOTIFICATION_INTERRUPT_ID;
811 res = lv1_open_device(dev.sbd.bus_id, dev.sbd.dev_id, 0);
813 pr_err("%s:%u: lv1_open_device failed %s\n", __func__,
814 __LINE__, ps3_result(res));
818 res = ps3_sb_event_receive_port_setup(&dev.sbd, PS3_BINDING_CPU_ANY,
821 pr_err("%s:%u: ps3_sb_event_receive_port_setup failed %d\n",
822 __func__, __LINE__, res);
823 goto fail_close_device;
826 spin_lock_init(&dev.lock);
828 res = request_irq(irq, ps3_notification_interrupt, IRQF_DISABLED,
829 "ps3_notification", &dev);
831 pr_err("%s:%u: request_irq failed %d\n", __func__, __LINE__,
833 goto fail_sb_event_receive_port_destroy;
836 /* Setup and write the request for device notification. */
837 notify_cmd->operation_code = 0; /* must be zero */
838 notify_cmd->event_mask = 1UL << notify_region_probe;
840 res = ps3_notification_read_write(&dev, lpar, 1);
844 /* Loop here processing the requested notification events. */
848 memset(notify_event, 0, sizeof(*notify_event));
850 res = ps3_notification_read_write(&dev, lpar, 0);
854 pr_debug("%s:%u: notify event type 0x%llx bus id %llu dev id %llu"
855 " type %llu port %llu\n", __func__, __LINE__,
856 notify_event->event_type, notify_event->bus_id,
857 notify_event->dev_id, notify_event->dev_type,
858 notify_event->dev_port);
860 if (notify_event->event_type != notify_region_probe ||
861 notify_event->bus_id != dev.sbd.bus_id) {
862 pr_warning("%s:%u: bad notify_event: event %llu, "
863 "dev_id %llu, dev_type %llu\n",
864 __func__, __LINE__, notify_event->event_type,
865 notify_event->dev_id,
866 notify_event->dev_type);
870 ps3_find_and_add_device(dev.sbd.bus_id, notify_event->dev_id);
872 } while (!kthread_should_stop());
876 fail_sb_event_receive_port_destroy:
877 ps3_sb_event_receive_port_destroy(&dev.sbd, irq);
879 lv1_close_device(dev.sbd.bus_id, dev.sbd.dev_id);
885 pr_debug(" <- %s:%u: kthread finished\n", __func__, __LINE__);
891 * ps3_stop_probe_thread - Stops the background probe thread.
895 static int ps3_stop_probe_thread(struct notifier_block *nb, unsigned long code,
899 kthread_stop(probe_task);
903 static struct notifier_block nb = {
904 .notifier_call = ps3_stop_probe_thread
908 * ps3_start_probe_thread - Starts the background probe thread.
912 static int __init ps3_start_probe_thread(enum ps3_bus_type bus_type)
915 struct task_struct *task;
916 struct ps3_repository_device repo;
918 pr_debug(" -> %s:%d\n", __func__, __LINE__);
920 memset(&repo, 0, sizeof(repo));
922 repo.bus_type = bus_type;
924 result = ps3_repository_find_bus(repo.bus_type, 0, &repo.bus_index);
927 printk(KERN_ERR "%s: Cannot find bus (%d)\n", __func__, result);
931 result = ps3_repository_read_bus_id(repo.bus_index, &repo.bus_id);
934 printk(KERN_ERR "%s: read_bus_id failed %d\n", __func__,
939 task = kthread_run(ps3_probe_thread, (void *)repo.bus_id,
940 "ps3-probe-%u", bus_type);
943 result = PTR_ERR(task);
944 printk(KERN_ERR "%s: kthread_run failed %d\n", __func__,
950 register_reboot_notifier(&nb);
952 pr_debug(" <- %s:%d\n", __func__, __LINE__);
957 * ps3_register_devices - Probe the system and register devices found.
959 * A device_initcall() routine.
962 static int __init ps3_register_devices(void)
966 if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
969 pr_debug(" -> %s:%d\n", __func__, __LINE__);
971 /* ps3_repository_dump_bus_info(); */
973 result = ps3_start_probe_thread(PS3_BUS_TYPE_STORAGE);
975 ps3_register_vuart_devices();
977 ps3_register_graphics_devices();
979 ps3_repository_find_devices(PS3_BUS_TYPE_SB, ps3_setup_static_device);
981 ps3_register_sound_devices();
983 ps3_register_lpm_devices();
985 ps3_register_ramdisk_device();
987 pr_debug(" <- %s:%d\n", __func__, __LINE__);
991 device_initcall(ps3_register_devices);