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>
35 * ps3_setup_gelic_device - Setup and register a gelic device instance.
37 * Allocates memory for a struct ps3_system_bus_device instance, initialises the
38 * structure members, and registers the device instance with the system bus.
41 static int __init ps3_setup_gelic_device(
42 const struct ps3_repository_device *repo)
46 struct ps3_system_bus_device dev;
47 struct ps3_dma_region d_region;
50 pr_debug(" -> %s:%d\n", __func__, __LINE__);
52 BUG_ON(repo->bus_type != PS3_BUS_TYPE_SB);
53 BUG_ON(repo->dev_type != PS3_DEV_TYPE_SB_GELIC);
55 p = kzalloc(sizeof(struct layout), GFP_KERNEL);
62 p->dev.match_id = PS3_MATCH_ID_GELIC;
63 p->dev.dev_type = PS3_DEVICE_TYPE_SB;
64 p->dev.bus_id = repo->bus_id;
65 p->dev.dev_id = repo->dev_id;
66 p->dev.d_region = &p->d_region;
68 result = ps3_repository_find_interrupt(repo,
69 PS3_INTERRUPT_TYPE_EVENT_PORT, &p->dev.interrupt_id);
72 pr_debug("%s:%d ps3_repository_find_interrupt failed\n",
74 goto fail_find_interrupt;
77 BUG_ON(p->dev.interrupt_id != 0);
79 result = ps3_dma_region_init(&p->dev, p->dev.d_region, PS3_DMA_64K,
80 PS3_DMA_OTHER, NULL, 0);
83 pr_debug("%s:%d ps3_dma_region_init failed\n",
88 result = ps3_system_bus_device_register(&p->dev);
91 pr_debug("%s:%d ps3_system_bus_device_register failed\n",
93 goto fail_device_register;
96 pr_debug(" <- %s:%d\n", __func__, __LINE__);
104 pr_debug(" <- %s:%d: fail.\n", __func__, __LINE__);
108 static int __init_refok ps3_setup_uhc_device(
109 const struct ps3_repository_device *repo, enum ps3_match_id match_id,
110 enum ps3_interrupt_type interrupt_type, enum ps3_reg_type reg_type)
114 struct ps3_system_bus_device dev;
115 struct ps3_dma_region d_region;
116 struct ps3_mmio_region m_region;
121 pr_debug(" -> %s:%d\n", __func__, __LINE__);
123 BUG_ON(repo->bus_type != PS3_BUS_TYPE_SB);
124 BUG_ON(repo->dev_type != PS3_DEV_TYPE_SB_USB);
126 p = kzalloc(sizeof(struct layout), GFP_KERNEL);
133 p->dev.match_id = match_id;
134 p->dev.dev_type = PS3_DEVICE_TYPE_SB;
135 p->dev.bus_id = repo->bus_id;
136 p->dev.dev_id = repo->dev_id;
137 p->dev.d_region = &p->d_region;
138 p->dev.m_region = &p->m_region;
140 result = ps3_repository_find_interrupt(repo,
141 interrupt_type, &p->dev.interrupt_id);
144 pr_debug("%s:%d ps3_repository_find_interrupt failed\n",
146 goto fail_find_interrupt;
149 result = ps3_repository_find_reg(repo, reg_type,
153 pr_debug("%s:%d ps3_repository_find_reg failed\n",
158 result = ps3_dma_region_init(&p->dev, p->dev.d_region, PS3_DMA_64K,
159 PS3_DMA_INTERNAL, NULL, 0);
162 pr_debug("%s:%d ps3_dma_region_init failed\n",
167 result = ps3_mmio_region_init(&p->dev, p->dev.m_region, bus_addr, len,
171 pr_debug("%s:%d ps3_mmio_region_init failed\n",
176 result = ps3_system_bus_device_register(&p->dev);
179 pr_debug("%s:%d ps3_system_bus_device_register failed\n",
181 goto fail_device_register;
184 pr_debug(" <- %s:%d\n", __func__, __LINE__);
187 fail_device_register:
194 pr_debug(" <- %s:%d: fail.\n", __func__, __LINE__);
198 static int __init ps3_setup_ehci_device(
199 const struct ps3_repository_device *repo)
201 return ps3_setup_uhc_device(repo, PS3_MATCH_ID_EHCI,
202 PS3_INTERRUPT_TYPE_SB_EHCI, PS3_REG_TYPE_SB_EHCI);
205 static int __init ps3_setup_ohci_device(
206 const struct ps3_repository_device *repo)
208 return ps3_setup_uhc_device(repo, PS3_MATCH_ID_OHCI,
209 PS3_INTERRUPT_TYPE_SB_OHCI, PS3_REG_TYPE_SB_OHCI);
212 static int __init ps3_setup_vuart_device(enum ps3_match_id match_id,
213 unsigned int port_number)
217 struct ps3_system_bus_device dev;
220 pr_debug(" -> %s:%d: match_id %u, port %u\n", __func__, __LINE__,
221 match_id, port_number);
223 p = kzalloc(sizeof(struct layout), GFP_KERNEL);
228 p->dev.match_id = match_id;
229 p->dev.dev_type = PS3_DEVICE_TYPE_VUART;
230 p->dev.port_number = port_number;
232 result = ps3_system_bus_device_register(&p->dev);
235 pr_debug("%s:%d ps3_system_bus_device_register failed\n",
238 pr_debug(" <- %s:%d\n", __func__, __LINE__);
242 static int ps3_setup_storage_dev(const struct ps3_repository_device *repo,
243 enum ps3_match_id match_id)
246 struct ps3_storage_device *p;
247 u64 port, blk_size, num_blocks;
248 unsigned int num_regions, i;
250 pr_debug(" -> %s:%u: match_id %u\n", __func__, __LINE__, match_id);
252 result = ps3_repository_read_stor_dev_info(repo->bus_index,
253 repo->dev_index, &port,
254 &blk_size, &num_blocks,
257 printk(KERN_ERR "%s:%u: _read_stor_dev_info failed %d\n",
258 __func__, __LINE__, result);
262 pr_debug("%s:%u: (%u:%u:%u): port %lu blk_size %lu num_blocks %lu "
263 "num_regions %u\n", __func__, __LINE__, repo->bus_index,
264 repo->dev_index, repo->dev_type, port, blk_size, num_blocks,
267 p = kzalloc(sizeof(struct ps3_storage_device) +
268 num_regions * sizeof(struct ps3_storage_region),
275 p->sbd.match_id = match_id;
276 p->sbd.dev_type = PS3_DEVICE_TYPE_SB;
277 p->sbd.bus_id = repo->bus_id;
278 p->sbd.dev_id = repo->dev_id;
279 p->sbd.d_region = &p->dma_region;
280 p->blk_size = blk_size;
281 p->num_regions = num_regions;
283 result = ps3_repository_find_interrupt(repo,
284 PS3_INTERRUPT_TYPE_EVENT_PORT,
285 &p->sbd.interrupt_id);
287 printk(KERN_ERR "%s:%u: find_interrupt failed %d\n", __func__,
290 goto fail_find_interrupt;
293 for (i = 0; i < num_regions; i++) {
297 result = ps3_repository_read_stor_dev_region(repo->bus_index,
303 "%s:%u: read_stor_dev_region failed %d\n",
304 __func__, __LINE__, result);
306 goto fail_read_region;
308 pr_debug("%s:%u: region %u: id %u start %lu size %lu\n",
309 __func__, __LINE__, i, id, start, size);
311 p->regions[i].id = id;
312 p->regions[i].start = start;
313 p->regions[i].size = size;
316 result = ps3_system_bus_device_register(&p->sbd);
318 pr_debug("%s:%u ps3_system_bus_device_register failed\n",
320 goto fail_device_register;
323 pr_debug(" <- %s:%u\n", __func__, __LINE__);
326 fail_device_register:
331 pr_debug(" <- %s:%u: fail.\n", __func__, __LINE__);
335 static int __init ps3_register_vuart_devices(void)
338 unsigned int port_number;
340 pr_debug(" -> %s:%d\n", __func__, __LINE__);
342 result = ps3_repository_read_vuart_av_port(&port_number);
344 port_number = 0; /* av default */
346 result = ps3_setup_vuart_device(PS3_MATCH_ID_AV_SETTINGS, port_number);
349 result = ps3_repository_read_vuart_sysmgr_port(&port_number);
351 port_number = 2; /* sysmgr default */
353 result = ps3_setup_vuart_device(PS3_MATCH_ID_SYSTEM_MANAGER,
357 pr_debug(" <- %s:%d\n", __func__, __LINE__);
361 static int __init ps3_register_sound_devices(void)
365 struct ps3_system_bus_device dev;
366 struct ps3_dma_region d_region;
367 struct ps3_mmio_region m_region;
370 pr_debug(" -> %s:%d\n", __func__, __LINE__);
372 p = kzalloc(sizeof(*p), GFP_KERNEL);
376 p->dev.match_id = PS3_MATCH_ID_SOUND;
377 p->dev.dev_type = PS3_DEVICE_TYPE_IOC0;
378 p->dev.d_region = &p->d_region;
379 p->dev.m_region = &p->m_region;
381 result = ps3_system_bus_device_register(&p->dev);
384 pr_debug("%s:%d ps3_system_bus_device_register failed\n",
387 pr_debug(" <- %s:%d\n", __func__, __LINE__);
391 static int __init ps3_register_graphics_devices(void)
395 struct ps3_system_bus_device dev;
398 pr_debug(" -> %s:%d\n", __func__, __LINE__);
400 p = kzalloc(sizeof(struct layout), GFP_KERNEL);
405 p->dev.match_id = PS3_MATCH_ID_GRAPHICS;
406 p->dev.dev_type = PS3_DEVICE_TYPE_IOC0;
408 result = ps3_system_bus_device_register(&p->dev);
411 pr_debug("%s:%d ps3_system_bus_device_register failed\n",
414 pr_debug(" <- %s:%d\n", __func__, __LINE__);
419 * ps3_register_repository_device - Register a device from the repositiory info.
423 static int ps3_register_repository_device(
424 const struct ps3_repository_device *repo)
428 switch (repo->dev_type) {
429 case PS3_DEV_TYPE_SB_GELIC:
430 result = ps3_setup_gelic_device(repo);
432 pr_debug("%s:%d ps3_setup_gelic_device failed\n",
436 case PS3_DEV_TYPE_SB_USB:
438 /* Each USB device has both an EHCI and an OHCI HC */
440 result = ps3_setup_ehci_device(repo);
443 pr_debug("%s:%d ps3_setup_ehci_device failed\n",
447 result = ps3_setup_ohci_device(repo);
450 pr_debug("%s:%d ps3_setup_ohci_device failed\n",
454 case PS3_DEV_TYPE_STOR_DISK:
455 result = ps3_setup_storage_dev(repo, PS3_MATCH_ID_STOR_DISK);
457 /* Some devices are not accessable from the Other OS lpar. */
458 if (result == -ENODEV) {
460 pr_debug("%s:%u: not accessable\n", __func__,
465 pr_debug("%s:%u ps3_setup_storage_dev failed\n",
469 case PS3_DEV_TYPE_STOR_ROM:
470 result = ps3_setup_storage_dev(repo, PS3_MATCH_ID_STOR_ROM);
472 pr_debug("%s:%u ps3_setup_storage_dev failed\n",
476 case PS3_DEV_TYPE_STOR_FLASH:
477 result = ps3_setup_storage_dev(repo, PS3_MATCH_ID_STOR_FLASH);
479 pr_debug("%s:%u ps3_setup_storage_dev failed\n",
485 pr_debug("%s:%u: unsupported dev_type %u\n", __func__, __LINE__,
492 static void ps3_find_and_add_device(u64 bus_id, u64 dev_id)
494 struct ps3_repository_device repo;
496 unsigned int retries;
500 * On some firmware versions (e.g. 1.90), the device may not show up
501 * in the repository immediately
503 for (retries = 0; retries < 10; retries++) {
504 res = ps3_repository_find_device_by_id(&repo, bus_id, dev_id);
508 rem = msleep_interruptible(100);
512 pr_warning("%s:%u: device %lu:%lu not found\n", __func__, __LINE__,
518 pr_debug("%s:%u: device %lu:%lu found after %u retries\n",
519 __func__, __LINE__, bus_id, dev_id, retries);
521 ps3_register_repository_device(&repo);
525 #define PS3_NOTIFICATION_DEV_ID ULONG_MAX
526 #define PS3_NOTIFICATION_INTERRUPT_ID 0
528 struct ps3_notification_device {
529 struct ps3_system_bus_device sbd;
533 struct completion done;
536 enum ps3_notify_type {
537 notify_device_ready = 0,
538 notify_region_probe = 1,
539 notify_region_update = 2,
542 struct ps3_notify_cmd {
543 u64 operation_code; /* must be zero */
544 u64 event_mask; /* OR of 1UL << enum ps3_notify_type */
547 struct ps3_notify_event {
548 u64 event_type; /* enum ps3_notify_type */
555 static irqreturn_t ps3_notification_interrupt(int irq, void *data)
557 struct ps3_notification_device *dev = data;
561 spin_lock(&dev->lock);
562 res = lv1_storage_get_async_status(PS3_NOTIFICATION_DEV_ID, &tag,
565 pr_err("%s:%u: tag mismatch, got %lx, expected %lx\n",
566 __func__, __LINE__, tag, dev->tag);
569 pr_err("%s:%u: res %d status 0x%lx\n", __func__, __LINE__, res,
572 pr_debug("%s:%u: completed, status 0x%lx\n", __func__,
574 dev->lv1_status = status;
575 complete(&dev->done);
577 spin_unlock(&dev->lock);
581 static int ps3_notification_read_write(struct ps3_notification_device *dev,
584 const char *op = write ? "write" : "read";
588 init_completion(&dev->done);
589 spin_lock_irqsave(&dev->lock, flags);
590 res = write ? lv1_storage_write(dev->sbd.dev_id, 0, 0, 1, 0, lpar,
592 : lv1_storage_read(dev->sbd.dev_id, 0, 0, 1, 0, lpar,
594 spin_unlock_irqrestore(&dev->lock, flags);
596 pr_err("%s:%u: %s failed %d\n", __func__, __LINE__, op, res);
599 pr_debug("%s:%u: notification %s issued\n", __func__, __LINE__, op);
601 res = wait_event_interruptible(dev->done.wait,
602 dev->done.done || kthread_should_stop());
603 if (kthread_should_stop())
606 pr_debug("%s:%u: interrupted %s\n", __func__, __LINE__, op);
610 if (dev->lv1_status) {
611 pr_err("%s:%u: %s not completed, status 0x%lx\n", __func__,
612 __LINE__, op, dev->lv1_status);
615 pr_debug("%s:%u: notification %s completed\n", __func__, __LINE__, op);
620 static struct task_struct *probe_task;
623 * ps3_probe_thread - Background repository probing at system startup.
625 * This implementation only supports background probing on a single bus.
626 * It uses the hypervisor's storage device notification mechanism to wait until
627 * a storage device is ready. The device notification mechanism uses a
628 * pseudo device to asynchronously notify the guest when storage devices become
629 * ready. The notification device has a block size of 512 bytes.
632 static int ps3_probe_thread(void *data)
634 struct ps3_notification_device dev;
639 struct ps3_notify_cmd *notify_cmd;
640 struct ps3_notify_event *notify_event;
642 pr_debug(" -> %s:%u: kthread started\n", __func__, __LINE__);
644 buf = kzalloc(512, GFP_KERNEL);
648 lpar = ps3_mm_phys_to_lpar(__pa(buf));
652 /* dummy system bus device */
653 dev.sbd.bus_id = (u64)data;
654 dev.sbd.dev_id = PS3_NOTIFICATION_DEV_ID;
655 dev.sbd.interrupt_id = PS3_NOTIFICATION_INTERRUPT_ID;
657 res = lv1_open_device(dev.sbd.bus_id, dev.sbd.dev_id, 0);
659 pr_err("%s:%u: lv1_open_device failed %s\n", __func__,
660 __LINE__, ps3_result(res));
664 res = ps3_sb_event_receive_port_setup(&dev.sbd, PS3_BINDING_CPU_ANY,
667 pr_err("%s:%u: ps3_sb_event_receive_port_setup failed %d\n",
668 __func__, __LINE__, res);
669 goto fail_close_device;
672 spin_lock_init(&dev.lock);
674 res = request_irq(irq, ps3_notification_interrupt, IRQF_DISABLED,
675 "ps3_notification", &dev);
677 pr_err("%s:%u: request_irq failed %d\n", __func__, __LINE__,
679 goto fail_sb_event_receive_port_destroy;
682 /* Setup and write the request for device notification. */
683 notify_cmd->operation_code = 0; /* must be zero */
684 notify_cmd->event_mask = 1UL << notify_region_probe;
686 res = ps3_notification_read_write(&dev, lpar, 1);
690 /* Loop here processing the requested notification events. */
694 memset(notify_event, 0, sizeof(*notify_event));
696 res = ps3_notification_read_write(&dev, lpar, 0);
700 pr_debug("%s:%u: notify event type 0x%lx bus id %lu dev id %lu"
701 " type %lu port %lu\n", __func__, __LINE__,
702 notify_event->event_type, notify_event->bus_id,
703 notify_event->dev_id, notify_event->dev_type,
704 notify_event->dev_port);
706 if (notify_event->event_type != notify_region_probe ||
707 notify_event->bus_id != dev.sbd.bus_id) {
708 pr_warning("%s:%u: bad notify_event: event %lu, "
709 "dev_id %lu, dev_type %lu\n",
710 __func__, __LINE__, notify_event->event_type,
711 notify_event->dev_id,
712 notify_event->dev_type);
716 ps3_find_and_add_device(dev.sbd.bus_id, notify_event->dev_id);
718 } while (!kthread_should_stop());
722 fail_sb_event_receive_port_destroy:
723 ps3_sb_event_receive_port_destroy(&dev.sbd, irq);
725 lv1_close_device(dev.sbd.bus_id, dev.sbd.dev_id);
731 pr_debug(" <- %s:%u: kthread finished\n", __func__, __LINE__);
737 * ps3_stop_probe_thread - Stops the background probe thread.
741 static int ps3_stop_probe_thread(struct notifier_block *nb, unsigned long code,
745 kthread_stop(probe_task);
749 static struct notifier_block nb = {
750 .notifier_call = ps3_stop_probe_thread
754 * ps3_start_probe_thread - Starts the background probe thread.
758 static int __init ps3_start_probe_thread(enum ps3_bus_type bus_type)
761 struct task_struct *task;
762 struct ps3_repository_device repo;
764 pr_debug(" -> %s:%d\n", __func__, __LINE__);
766 memset(&repo, 0, sizeof(repo));
768 repo.bus_type = bus_type;
770 result = ps3_repository_find_bus(repo.bus_type, 0, &repo.bus_index);
773 printk(KERN_ERR "%s: Cannot find bus (%d)\n", __func__, result);
777 result = ps3_repository_read_bus_id(repo.bus_index, &repo.bus_id);
780 printk(KERN_ERR "%s: read_bus_id failed %d\n", __func__,
785 task = kthread_run(ps3_probe_thread, (void *)repo.bus_id,
786 "ps3-probe-%u", bus_type);
789 result = PTR_ERR(task);
790 printk(KERN_ERR "%s: kthread_run failed %d\n", __func__,
796 register_reboot_notifier(&nb);
798 pr_debug(" <- %s:%d\n", __func__, __LINE__);
803 * ps3_register_devices - Probe the system and register devices found.
805 * A device_initcall() routine.
808 static int __init ps3_register_devices(void)
812 if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
815 pr_debug(" -> %s:%d\n", __func__, __LINE__);
817 /* ps3_repository_dump_bus_info(); */
819 result = ps3_start_probe_thread(PS3_BUS_TYPE_STORAGE);
821 ps3_register_vuart_devices();
823 ps3_register_graphics_devices();
825 ps3_repository_find_devices(PS3_BUS_TYPE_SB,
826 ps3_register_repository_device);
828 ps3_register_sound_devices();
830 pr_debug(" <- %s:%d\n", __func__, __LINE__);
834 device_initcall(ps3_register_devices);