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/reboot.h>
28 #include <asm/firmware.h>
29 #include <asm/lv1call.h>
30 #include <asm/ps3stor.h>
34 static int __init ps3_register_lpm_devices(void)
39 struct ps3_system_bus_device *dev;
41 pr_debug(" -> %s:%d\n", __func__, __LINE__);
43 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
47 dev->match_id = PS3_MATCH_ID_LPM;
48 dev->dev_type = PS3_DEVICE_TYPE_LPM;
50 /* The current lpm driver only supports a single BE processor. */
52 result = ps3_repository_read_be_node_id(0, &dev->lpm.node_id);
55 pr_debug("%s:%d: ps3_repository_read_be_node_id failed \n",
60 result = ps3_repository_read_lpm_privileges(dev->lpm.node_id, &tmp1,
64 pr_debug("%s:%d: ps3_repository_read_lpm_privleges failed \n",
69 lv1_get_logical_partition_id(&tmp2);
72 pr_debug("%s:%d: wrong lpar\n",
78 if (!(dev->lpm.rights & PS3_LPM_RIGHTS_USE_LPM)) {
79 pr_debug("%s:%d: don't have rights to use lpm\n",
85 pr_debug("%s:%d: pu_id %lu, rights %lu(%lxh)\n",
86 __func__, __LINE__, dev->lpm.pu_id, dev->lpm.rights,
89 result = ps3_repository_read_pu_id(0, &dev->lpm.pu_id);
92 pr_debug("%s:%d: ps3_repository_read_pu_id failed \n",
97 result = ps3_system_bus_device_register(dev);
100 pr_debug("%s:%d ps3_system_bus_device_register failed\n",
105 pr_debug(" <- %s:%d\n", __func__, __LINE__);
113 pr_debug(" <- %s:%d: failed\n", __func__, __LINE__);
118 * ps3_setup_gelic_device - Setup and register a gelic device instance.
120 * Allocates memory for a struct ps3_system_bus_device instance, initialises the
121 * structure members, and registers the device instance with the system bus.
124 static int __init ps3_setup_gelic_device(
125 const struct ps3_repository_device *repo)
129 struct ps3_system_bus_device dev;
130 struct ps3_dma_region d_region;
133 pr_debug(" -> %s:%d\n", __func__, __LINE__);
135 BUG_ON(repo->bus_type != PS3_BUS_TYPE_SB);
136 BUG_ON(repo->dev_type != PS3_DEV_TYPE_SB_GELIC);
138 p = kzalloc(sizeof(struct layout), GFP_KERNEL);
145 p->dev.match_id = PS3_MATCH_ID_GELIC;
146 p->dev.dev_type = PS3_DEVICE_TYPE_SB;
147 p->dev.bus_id = repo->bus_id;
148 p->dev.dev_id = repo->dev_id;
149 p->dev.d_region = &p->d_region;
151 result = ps3_repository_find_interrupt(repo,
152 PS3_INTERRUPT_TYPE_EVENT_PORT, &p->dev.interrupt_id);
155 pr_debug("%s:%d ps3_repository_find_interrupt failed\n",
157 goto fail_find_interrupt;
160 BUG_ON(p->dev.interrupt_id != 0);
162 result = ps3_dma_region_init(&p->dev, p->dev.d_region, PS3_DMA_64K,
163 PS3_DMA_OTHER, NULL, 0);
166 pr_debug("%s:%d ps3_dma_region_init failed\n",
171 result = ps3_system_bus_device_register(&p->dev);
174 pr_debug("%s:%d ps3_system_bus_device_register failed\n",
176 goto fail_device_register;
179 pr_debug(" <- %s:%d\n", __func__, __LINE__);
182 fail_device_register:
187 pr_debug(" <- %s:%d: fail.\n", __func__, __LINE__);
191 static int __init_refok ps3_setup_uhc_device(
192 const struct ps3_repository_device *repo, enum ps3_match_id match_id,
193 enum ps3_interrupt_type interrupt_type, enum ps3_reg_type reg_type)
197 struct ps3_system_bus_device dev;
198 struct ps3_dma_region d_region;
199 struct ps3_mmio_region m_region;
204 pr_debug(" -> %s:%d\n", __func__, __LINE__);
206 BUG_ON(repo->bus_type != PS3_BUS_TYPE_SB);
207 BUG_ON(repo->dev_type != PS3_DEV_TYPE_SB_USB);
209 p = kzalloc(sizeof(struct layout), GFP_KERNEL);
216 p->dev.match_id = match_id;
217 p->dev.dev_type = PS3_DEVICE_TYPE_SB;
218 p->dev.bus_id = repo->bus_id;
219 p->dev.dev_id = repo->dev_id;
220 p->dev.d_region = &p->d_region;
221 p->dev.m_region = &p->m_region;
223 result = ps3_repository_find_interrupt(repo,
224 interrupt_type, &p->dev.interrupt_id);
227 pr_debug("%s:%d ps3_repository_find_interrupt failed\n",
229 goto fail_find_interrupt;
232 result = ps3_repository_find_reg(repo, reg_type,
236 pr_debug("%s:%d ps3_repository_find_reg failed\n",
241 result = ps3_dma_region_init(&p->dev, p->dev.d_region, PS3_DMA_64K,
242 PS3_DMA_INTERNAL, NULL, 0);
245 pr_debug("%s:%d ps3_dma_region_init failed\n",
250 result = ps3_mmio_region_init(&p->dev, p->dev.m_region, bus_addr, len,
254 pr_debug("%s:%d ps3_mmio_region_init failed\n",
259 result = ps3_system_bus_device_register(&p->dev);
262 pr_debug("%s:%d ps3_system_bus_device_register failed\n",
264 goto fail_device_register;
267 pr_debug(" <- %s:%d\n", __func__, __LINE__);
270 fail_device_register:
277 pr_debug(" <- %s:%d: fail.\n", __func__, __LINE__);
281 static int __init ps3_setup_ehci_device(
282 const struct ps3_repository_device *repo)
284 return ps3_setup_uhc_device(repo, PS3_MATCH_ID_EHCI,
285 PS3_INTERRUPT_TYPE_SB_EHCI, PS3_REG_TYPE_SB_EHCI);
288 static int __init ps3_setup_ohci_device(
289 const struct ps3_repository_device *repo)
291 return ps3_setup_uhc_device(repo, PS3_MATCH_ID_OHCI,
292 PS3_INTERRUPT_TYPE_SB_OHCI, PS3_REG_TYPE_SB_OHCI);
295 static int __init ps3_setup_vuart_device(enum ps3_match_id match_id,
296 unsigned int port_number)
300 struct ps3_system_bus_device dev;
303 pr_debug(" -> %s:%d: match_id %u, port %u\n", __func__, __LINE__,
304 match_id, port_number);
306 p = kzalloc(sizeof(struct layout), GFP_KERNEL);
311 p->dev.match_id = match_id;
312 p->dev.dev_type = PS3_DEVICE_TYPE_VUART;
313 p->dev.port_number = port_number;
315 result = ps3_system_bus_device_register(&p->dev);
318 pr_debug("%s:%d ps3_system_bus_device_register failed\n",
321 pr_debug(" <- %s:%d\n", __func__, __LINE__);
325 static int ps3_setup_storage_dev(const struct ps3_repository_device *repo,
326 enum ps3_match_id match_id)
329 struct ps3_storage_device *p;
330 u64 port, blk_size, num_blocks;
331 unsigned int num_regions, i;
333 pr_debug(" -> %s:%u: match_id %u\n", __func__, __LINE__, match_id);
335 result = ps3_repository_read_stor_dev_info(repo->bus_index,
336 repo->dev_index, &port,
337 &blk_size, &num_blocks,
340 printk(KERN_ERR "%s:%u: _read_stor_dev_info failed %d\n",
341 __func__, __LINE__, result);
345 pr_debug("%s:%u: (%u:%u:%u): port %lu blk_size %lu num_blocks %lu "
346 "num_regions %u\n", __func__, __LINE__, repo->bus_index,
347 repo->dev_index, repo->dev_type, port, blk_size, num_blocks,
350 p = kzalloc(sizeof(struct ps3_storage_device) +
351 num_regions * sizeof(struct ps3_storage_region),
358 p->sbd.match_id = match_id;
359 p->sbd.dev_type = PS3_DEVICE_TYPE_SB;
360 p->sbd.bus_id = repo->bus_id;
361 p->sbd.dev_id = repo->dev_id;
362 p->sbd.d_region = &p->dma_region;
363 p->blk_size = blk_size;
364 p->num_regions = num_regions;
366 result = ps3_repository_find_interrupt(repo,
367 PS3_INTERRUPT_TYPE_EVENT_PORT,
368 &p->sbd.interrupt_id);
370 printk(KERN_ERR "%s:%u: find_interrupt failed %d\n", __func__,
373 goto fail_find_interrupt;
376 for (i = 0; i < num_regions; i++) {
380 result = ps3_repository_read_stor_dev_region(repo->bus_index,
386 "%s:%u: read_stor_dev_region failed %d\n",
387 __func__, __LINE__, result);
389 goto fail_read_region;
391 pr_debug("%s:%u: region %u: id %u start %lu size %lu\n",
392 __func__, __LINE__, i, id, start, size);
394 p->regions[i].id = id;
395 p->regions[i].start = start;
396 p->regions[i].size = size;
399 result = ps3_system_bus_device_register(&p->sbd);
401 pr_debug("%s:%u ps3_system_bus_device_register failed\n",
403 goto fail_device_register;
406 pr_debug(" <- %s:%u\n", __func__, __LINE__);
409 fail_device_register:
414 pr_debug(" <- %s:%u: fail.\n", __func__, __LINE__);
418 static int __init ps3_register_vuart_devices(void)
421 unsigned int port_number;
423 pr_debug(" -> %s:%d\n", __func__, __LINE__);
425 result = ps3_repository_read_vuart_av_port(&port_number);
427 port_number = 0; /* av default */
429 result = ps3_setup_vuart_device(PS3_MATCH_ID_AV_SETTINGS, port_number);
432 result = ps3_repository_read_vuart_sysmgr_port(&port_number);
434 port_number = 2; /* sysmgr default */
436 result = ps3_setup_vuart_device(PS3_MATCH_ID_SYSTEM_MANAGER,
440 pr_debug(" <- %s:%d\n", __func__, __LINE__);
444 static int __init ps3_register_sound_devices(void)
448 struct ps3_system_bus_device dev;
449 struct ps3_dma_region d_region;
450 struct ps3_mmio_region m_region;
453 pr_debug(" -> %s:%d\n", __func__, __LINE__);
455 p = kzalloc(sizeof(*p), GFP_KERNEL);
459 p->dev.match_id = PS3_MATCH_ID_SOUND;
460 p->dev.dev_type = PS3_DEVICE_TYPE_IOC0;
461 p->dev.d_region = &p->d_region;
462 p->dev.m_region = &p->m_region;
464 result = ps3_system_bus_device_register(&p->dev);
467 pr_debug("%s:%d ps3_system_bus_device_register failed\n",
470 pr_debug(" <- %s:%d\n", __func__, __LINE__);
474 static int __init ps3_register_graphics_devices(void)
478 struct ps3_system_bus_device dev;
481 pr_debug(" -> %s:%d\n", __func__, __LINE__);
483 p = kzalloc(sizeof(struct layout), GFP_KERNEL);
488 p->dev.match_id = PS3_MATCH_ID_GRAPHICS;
489 p->dev.match_sub_id = PS3_MATCH_SUB_ID_FB;
490 p->dev.dev_type = PS3_DEVICE_TYPE_IOC0;
492 result = ps3_system_bus_device_register(&p->dev);
495 pr_debug("%s:%d ps3_system_bus_device_register failed\n",
498 pr_debug(" <- %s:%d\n", __func__, __LINE__);
503 * ps3_setup_dynamic_device - Setup a dynamic device from the repository
506 static int ps3_setup_dynamic_device(const struct ps3_repository_device *repo)
510 switch (repo->dev_type) {
511 case PS3_DEV_TYPE_STOR_DISK:
512 result = ps3_setup_storage_dev(repo, PS3_MATCH_ID_STOR_DISK);
514 /* Some devices are not accessable from the Other OS lpar. */
515 if (result == -ENODEV) {
517 pr_debug("%s:%u: not accessable\n", __func__,
522 pr_debug("%s:%u ps3_setup_storage_dev failed\n",
526 case PS3_DEV_TYPE_STOR_ROM:
527 result = ps3_setup_storage_dev(repo, PS3_MATCH_ID_STOR_ROM);
529 pr_debug("%s:%u ps3_setup_storage_dev failed\n",
533 case PS3_DEV_TYPE_STOR_FLASH:
534 result = ps3_setup_storage_dev(repo, PS3_MATCH_ID_STOR_FLASH);
536 pr_debug("%s:%u ps3_setup_storage_dev failed\n",
542 pr_debug("%s:%u: unsupported dev_type %u\n", __func__, __LINE__,
550 * ps3_setup_static_device - Setup a static device from the repository
553 static int __init ps3_setup_static_device(const struct ps3_repository_device *repo)
557 switch (repo->dev_type) {
558 case PS3_DEV_TYPE_SB_GELIC:
559 result = ps3_setup_gelic_device(repo);
561 pr_debug("%s:%d ps3_setup_gelic_device failed\n",
565 case PS3_DEV_TYPE_SB_USB:
567 /* Each USB device has both an EHCI and an OHCI HC */
569 result = ps3_setup_ehci_device(repo);
572 pr_debug("%s:%d ps3_setup_ehci_device failed\n",
576 result = ps3_setup_ohci_device(repo);
579 pr_debug("%s:%d ps3_setup_ohci_device failed\n",
585 return ps3_setup_dynamic_device(repo);
591 static void ps3_find_and_add_device(u64 bus_id, u64 dev_id)
593 struct ps3_repository_device repo;
595 unsigned int retries;
599 * On some firmware versions (e.g. 1.90), the device may not show up
600 * in the repository immediately
602 for (retries = 0; retries < 10; retries++) {
603 res = ps3_repository_find_device_by_id(&repo, bus_id, dev_id);
607 rem = msleep_interruptible(100);
611 pr_warning("%s:%u: device %lu:%lu not found\n", __func__, __LINE__,
617 pr_debug("%s:%u: device %lu:%lu found after %u retries\n",
618 __func__, __LINE__, bus_id, dev_id, retries);
620 ps3_setup_dynamic_device(&repo);
624 #define PS3_NOTIFICATION_DEV_ID ULONG_MAX
625 #define PS3_NOTIFICATION_INTERRUPT_ID 0
627 struct ps3_notification_device {
628 struct ps3_system_bus_device sbd;
632 struct completion done;
635 enum ps3_notify_type {
636 notify_device_ready = 0,
637 notify_region_probe = 1,
638 notify_region_update = 2,
641 struct ps3_notify_cmd {
642 u64 operation_code; /* must be zero */
643 u64 event_mask; /* OR of 1UL << enum ps3_notify_type */
646 struct ps3_notify_event {
647 u64 event_type; /* enum ps3_notify_type */
654 static irqreturn_t ps3_notification_interrupt(int irq, void *data)
656 struct ps3_notification_device *dev = data;
660 spin_lock(&dev->lock);
661 res = lv1_storage_get_async_status(PS3_NOTIFICATION_DEV_ID, &tag,
664 pr_err("%s:%u: tag mismatch, got %lx, expected %lx\n",
665 __func__, __LINE__, tag, dev->tag);
668 pr_err("%s:%u: res %d status 0x%lx\n", __func__, __LINE__, res,
671 pr_debug("%s:%u: completed, status 0x%lx\n", __func__,
673 dev->lv1_status = status;
674 complete(&dev->done);
676 spin_unlock(&dev->lock);
680 static int ps3_notification_read_write(struct ps3_notification_device *dev,
683 const char *op = write ? "write" : "read";
687 init_completion(&dev->done);
688 spin_lock_irqsave(&dev->lock, flags);
689 res = write ? lv1_storage_write(dev->sbd.dev_id, 0, 0, 1, 0, lpar,
691 : lv1_storage_read(dev->sbd.dev_id, 0, 0, 1, 0, lpar,
693 spin_unlock_irqrestore(&dev->lock, flags);
695 pr_err("%s:%u: %s failed %d\n", __func__, __LINE__, op, res);
698 pr_debug("%s:%u: notification %s issued\n", __func__, __LINE__, op);
700 res = wait_event_interruptible(dev->done.wait,
701 dev->done.done || kthread_should_stop());
702 if (kthread_should_stop())
705 pr_debug("%s:%u: interrupted %s\n", __func__, __LINE__, op);
709 if (dev->lv1_status) {
710 pr_err("%s:%u: %s not completed, status 0x%lx\n", __func__,
711 __LINE__, op, dev->lv1_status);
714 pr_debug("%s:%u: notification %s completed\n", __func__, __LINE__, op);
719 static struct task_struct *probe_task;
722 * ps3_probe_thread - Background repository probing at system startup.
724 * This implementation only supports background probing on a single bus.
725 * It uses the hypervisor's storage device notification mechanism to wait until
726 * a storage device is ready. The device notification mechanism uses a
727 * pseudo device to asynchronously notify the guest when storage devices become
728 * ready. The notification device has a block size of 512 bytes.
731 static int ps3_probe_thread(void *data)
733 struct ps3_notification_device dev;
738 struct ps3_notify_cmd *notify_cmd;
739 struct ps3_notify_event *notify_event;
741 pr_debug(" -> %s:%u: kthread started\n", __func__, __LINE__);
743 buf = kzalloc(512, GFP_KERNEL);
747 lpar = ps3_mm_phys_to_lpar(__pa(buf));
751 /* dummy system bus device */
752 dev.sbd.bus_id = (u64)data;
753 dev.sbd.dev_id = PS3_NOTIFICATION_DEV_ID;
754 dev.sbd.interrupt_id = PS3_NOTIFICATION_INTERRUPT_ID;
756 res = lv1_open_device(dev.sbd.bus_id, dev.sbd.dev_id, 0);
758 pr_err("%s:%u: lv1_open_device failed %s\n", __func__,
759 __LINE__, ps3_result(res));
763 res = ps3_sb_event_receive_port_setup(&dev.sbd, PS3_BINDING_CPU_ANY,
766 pr_err("%s:%u: ps3_sb_event_receive_port_setup failed %d\n",
767 __func__, __LINE__, res);
768 goto fail_close_device;
771 spin_lock_init(&dev.lock);
773 res = request_irq(irq, ps3_notification_interrupt, IRQF_DISABLED,
774 "ps3_notification", &dev);
776 pr_err("%s:%u: request_irq failed %d\n", __func__, __LINE__,
778 goto fail_sb_event_receive_port_destroy;
781 /* Setup and write the request for device notification. */
782 notify_cmd->operation_code = 0; /* must be zero */
783 notify_cmd->event_mask = 1UL << notify_region_probe;
785 res = ps3_notification_read_write(&dev, lpar, 1);
789 /* Loop here processing the requested notification events. */
793 memset(notify_event, 0, sizeof(*notify_event));
795 res = ps3_notification_read_write(&dev, lpar, 0);
799 pr_debug("%s:%u: notify event type 0x%lx bus id %lu dev id %lu"
800 " type %lu port %lu\n", __func__, __LINE__,
801 notify_event->event_type, notify_event->bus_id,
802 notify_event->dev_id, notify_event->dev_type,
803 notify_event->dev_port);
805 if (notify_event->event_type != notify_region_probe ||
806 notify_event->bus_id != dev.sbd.bus_id) {
807 pr_warning("%s:%u: bad notify_event: event %lu, "
808 "dev_id %lu, dev_type %lu\n",
809 __func__, __LINE__, notify_event->event_type,
810 notify_event->dev_id,
811 notify_event->dev_type);
815 ps3_find_and_add_device(dev.sbd.bus_id, notify_event->dev_id);
817 } while (!kthread_should_stop());
821 fail_sb_event_receive_port_destroy:
822 ps3_sb_event_receive_port_destroy(&dev.sbd, irq);
824 lv1_close_device(dev.sbd.bus_id, dev.sbd.dev_id);
830 pr_debug(" <- %s:%u: kthread finished\n", __func__, __LINE__);
836 * ps3_stop_probe_thread - Stops the background probe thread.
840 static int ps3_stop_probe_thread(struct notifier_block *nb, unsigned long code,
844 kthread_stop(probe_task);
848 static struct notifier_block nb = {
849 .notifier_call = ps3_stop_probe_thread
853 * ps3_start_probe_thread - Starts the background probe thread.
857 static int __init ps3_start_probe_thread(enum ps3_bus_type bus_type)
860 struct task_struct *task;
861 struct ps3_repository_device repo;
863 pr_debug(" -> %s:%d\n", __func__, __LINE__);
865 memset(&repo, 0, sizeof(repo));
867 repo.bus_type = bus_type;
869 result = ps3_repository_find_bus(repo.bus_type, 0, &repo.bus_index);
872 printk(KERN_ERR "%s: Cannot find bus (%d)\n", __func__, result);
876 result = ps3_repository_read_bus_id(repo.bus_index, &repo.bus_id);
879 printk(KERN_ERR "%s: read_bus_id failed %d\n", __func__,
884 task = kthread_run(ps3_probe_thread, (void *)repo.bus_id,
885 "ps3-probe-%u", bus_type);
888 result = PTR_ERR(task);
889 printk(KERN_ERR "%s: kthread_run failed %d\n", __func__,
895 register_reboot_notifier(&nb);
897 pr_debug(" <- %s:%d\n", __func__, __LINE__);
902 * ps3_register_devices - Probe the system and register devices found.
904 * A device_initcall() routine.
907 static int __init ps3_register_devices(void)
911 if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
914 pr_debug(" -> %s:%d\n", __func__, __LINE__);
916 /* ps3_repository_dump_bus_info(); */
918 result = ps3_start_probe_thread(PS3_BUS_TYPE_STORAGE);
920 ps3_register_vuart_devices();
922 ps3_register_graphics_devices();
924 ps3_repository_find_devices(PS3_BUS_TYPE_SB, ps3_setup_static_device);
926 ps3_register_sound_devices();
928 ps3_register_lpm_devices();
930 pr_debug(" <- %s:%d\n", __func__, __LINE__);
934 device_initcall(ps3_register_devices);