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>
27 #include <asm/firmware.h>
28 #include <asm/lv1call.h>
29 #include <asm/ps3stor.h>
34 * ps3_setup_gelic_device - Setup and register a gelic device instance.
36 * Allocates memory for a struct ps3_system_bus_device instance, initialises the
37 * structure members, and registers the device instance with the system bus.
40 static int __init ps3_setup_gelic_device(
41 const struct ps3_repository_device *repo)
45 struct ps3_system_bus_device dev;
46 struct ps3_dma_region d_region;
49 pr_debug(" -> %s:%d\n", __func__, __LINE__);
51 BUG_ON(repo->bus_type != PS3_BUS_TYPE_SB);
52 BUG_ON(repo->dev_type != PS3_DEV_TYPE_SB_GELIC);
54 p = kzalloc(sizeof(struct layout), GFP_KERNEL);
61 p->dev.match_id = PS3_MATCH_ID_GELIC;
62 p->dev.dev_type = PS3_DEVICE_TYPE_SB;
63 p->dev.bus_id = repo->bus_id;
64 p->dev.dev_id = repo->dev_id;
65 p->dev.d_region = &p->d_region;
67 result = ps3_repository_find_interrupt(repo,
68 PS3_INTERRUPT_TYPE_EVENT_PORT, &p->dev.interrupt_id);
71 pr_debug("%s:%d ps3_repository_find_interrupt failed\n",
73 goto fail_find_interrupt;
76 BUG_ON(p->dev.interrupt_id != 0);
78 result = ps3_dma_region_init(&p->dev, p->dev.d_region, PS3_DMA_64K,
79 PS3_DMA_OTHER, NULL, 0);
82 pr_debug("%s:%d ps3_dma_region_init failed\n",
87 result = ps3_system_bus_device_register(&p->dev);
90 pr_debug("%s:%d ps3_system_bus_device_register failed\n",
92 goto fail_device_register;
95 pr_debug(" <- %s:%d\n", __func__, __LINE__);
103 pr_debug(" <- %s:%d: fail.\n", __func__, __LINE__);
107 static int __init_refok ps3_setup_uhc_device(
108 const struct ps3_repository_device *repo, enum ps3_match_id match_id,
109 enum ps3_interrupt_type interrupt_type, enum ps3_reg_type reg_type)
113 struct ps3_system_bus_device dev;
114 struct ps3_dma_region d_region;
115 struct ps3_mmio_region m_region;
120 pr_debug(" -> %s:%d\n", __func__, __LINE__);
122 BUG_ON(repo->bus_type != PS3_BUS_TYPE_SB);
123 BUG_ON(repo->dev_type != PS3_DEV_TYPE_SB_USB);
125 p = kzalloc(sizeof(struct layout), GFP_KERNEL);
132 p->dev.match_id = match_id;
133 p->dev.dev_type = PS3_DEVICE_TYPE_SB;
134 p->dev.bus_id = repo->bus_id;
135 p->dev.dev_id = repo->dev_id;
136 p->dev.d_region = &p->d_region;
137 p->dev.m_region = &p->m_region;
139 result = ps3_repository_find_interrupt(repo,
140 interrupt_type, &p->dev.interrupt_id);
143 pr_debug("%s:%d ps3_repository_find_interrupt failed\n",
145 goto fail_find_interrupt;
148 result = ps3_repository_find_reg(repo, reg_type,
152 pr_debug("%s:%d ps3_repository_find_reg failed\n",
157 result = ps3_dma_region_init(&p->dev, p->dev.d_region, PS3_DMA_64K,
158 PS3_DMA_INTERNAL, NULL, 0);
161 pr_debug("%s:%d ps3_dma_region_init failed\n",
166 result = ps3_mmio_region_init(&p->dev, p->dev.m_region, bus_addr, len,
170 pr_debug("%s:%d ps3_mmio_region_init failed\n",
175 result = ps3_system_bus_device_register(&p->dev);
178 pr_debug("%s:%d ps3_system_bus_device_register failed\n",
180 goto fail_device_register;
183 pr_debug(" <- %s:%d\n", __func__, __LINE__);
186 fail_device_register:
193 pr_debug(" <- %s:%d: fail.\n", __func__, __LINE__);
197 static int __init ps3_setup_ehci_device(
198 const struct ps3_repository_device *repo)
200 return ps3_setup_uhc_device(repo, PS3_MATCH_ID_EHCI,
201 PS3_INTERRUPT_TYPE_SB_EHCI, PS3_REG_TYPE_SB_EHCI);
204 static int __init ps3_setup_ohci_device(
205 const struct ps3_repository_device *repo)
207 return ps3_setup_uhc_device(repo, PS3_MATCH_ID_OHCI,
208 PS3_INTERRUPT_TYPE_SB_OHCI, PS3_REG_TYPE_SB_OHCI);
211 static int __init ps3_setup_vuart_device(enum ps3_match_id match_id,
212 unsigned int port_number)
216 struct ps3_system_bus_device dev;
219 pr_debug(" -> %s:%d: match_id %u, port %u\n", __func__, __LINE__,
220 match_id, port_number);
222 p = kzalloc(sizeof(struct layout), GFP_KERNEL);
227 p->dev.match_id = match_id;
228 p->dev.dev_type = PS3_DEVICE_TYPE_VUART;
229 p->dev.port_number = port_number;
231 result = ps3_system_bus_device_register(&p->dev);
234 pr_debug("%s:%d ps3_system_bus_device_register failed\n",
237 pr_debug(" <- %s:%d\n", __func__, __LINE__);
241 static int ps3stor_wait_for_completion(u64 dev_id, u64 tag,
242 unsigned int timeout)
245 unsigned int retries = 0;
248 for (retries = 0; retries < timeout; retries++) {
249 result = lv1_storage_check_async_status(dev_id, tag, &status);
257 pr_debug("%s:%u: check_async_status: %s, status %lx\n",
258 __func__, __LINE__, ps3_result(result), status);
264 * ps3_storage_wait_for_device - Wait for a storage device to become ready.
265 * @repo: The repository device to wait for.
267 * Uses the hypervisor's storage device notification mechanism to wait until
268 * a storage device is ready. The device notification mechanism uses a
269 * psuedo device (id = -1) to asynchronously notify the guest when storage
270 * devices become ready. The notification device has a block size of 512
274 static int ps3_storage_wait_for_device(const struct ps3_repository_device *repo)
278 const u64 notification_dev_id = (u64)-1LL;
279 const unsigned int timeout = HZ;
283 enum ps3_notify_type {
284 notify_device_ready = 0,
285 notify_region_probe = 1,
286 notify_region_update = 2,
289 u64 operation_code; /* must be zero */
290 u64 event_mask; /* OR of 1UL << enum ps3_notify_type */
293 u64 event_type; /* enum ps3_notify_type */
300 pr_debug(" -> %s:%u: (%u:%u:%u)\n", __func__, __LINE__, repo->bus_id,
301 repo->dev_id, repo->dev_type);
303 buf = kzalloc(512, GFP_KERNEL);
307 lpar = ps3_mm_phys_to_lpar(__pa(buf));
311 result = lv1_open_device(repo->bus_id, notification_dev_id, 0);
313 printk(KERN_ERR "%s:%u: lv1_open_device %s\n", __func__,
314 __LINE__, ps3_result(result));
318 /* Setup and write the request for device notification. */
320 notify_cmd->operation_code = 0; /* must be zero */
321 notify_cmd->event_mask = 1UL << notify_region_probe;
323 result = lv1_storage_write(notification_dev_id, 0, 0, 1, 0, lpar,
326 printk(KERN_ERR "%s:%u: write failed %s\n", __func__, __LINE__,
331 /* Wait for the write completion */
333 result = ps3stor_wait_for_completion(notification_dev_id, tag,
336 printk(KERN_ERR "%s:%u: write not completed %s\n", __func__,
337 __LINE__, ps3_result(result));
341 /* Loop here processing the requested notification events. */
344 memset(notify_event, 0, sizeof(*notify_event));
346 result = lv1_storage_read(notification_dev_id, 0, 0, 1, 0,
349 printk(KERN_ERR "%s:%u: write failed %s\n", __func__,
350 __LINE__, ps3_result(result));
354 result = ps3stor_wait_for_completion(notification_dev_id, tag,
357 printk(KERN_ERR "%s:%u: read not completed %s\n",
358 __func__, __LINE__, ps3_result(result));
362 pr_debug("%s:%d: notify event (%u:%u:%u): event_type 0x%lx, "
363 "port %lu\n", __func__, __LINE__, repo->bus_index,
364 repo->dev_index, repo->dev_type,
365 notify_event->event_type, notify_event->dev_port);
367 if (notify_event->event_type != notify_region_probe ||
368 notify_event->bus_id != repo->bus_id) {
369 pr_debug("%s:%u: bad notify_event: event %lu, "
370 "dev_id %lu, dev_type %lu\n",
371 __func__, __LINE__, notify_event->event_type,
372 notify_event->dev_id, notify_event->dev_type);
376 if (notify_event->dev_id == repo->dev_id &&
377 notify_event->dev_type == repo->dev_type) {
378 pr_debug("%s:%u: device ready (%u:%u:%u)\n", __func__,
379 __LINE__, repo->bus_index, repo->dev_index,
385 if (notify_event->dev_id == repo->dev_id &&
386 notify_event->dev_type == PS3_DEV_TYPE_NOACCESS) {
387 pr_debug("%s:%u: no access: dev_id %u\n", __func__,
388 __LINE__, repo->dev_id);
394 lv1_close_device(repo->bus_id, notification_dev_id);
397 pr_debug(" <- %s:%u\n", __func__, __LINE__);
401 static int ps3_setup_storage_dev(const struct ps3_repository_device *repo,
402 enum ps3_match_id match_id)
405 struct ps3_storage_device *p;
406 u64 port, blk_size, num_blocks;
407 unsigned int num_regions, i;
409 pr_debug(" -> %s:%u: match_id %u\n", __func__, __LINE__, match_id);
411 result = ps3_repository_read_stor_dev_info(repo->bus_index,
412 repo->dev_index, &port,
413 &blk_size, &num_blocks,
416 printk(KERN_ERR "%s:%u: _read_stor_dev_info failed %d\n",
417 __func__, __LINE__, result);
421 pr_debug("%s:%u: (%u:%u:%u): port %lu blk_size %lu num_blocks %lu "
422 "num_regions %u\n", __func__, __LINE__, repo->bus_index,
423 repo->dev_index, repo->dev_type, port, blk_size, num_blocks,
426 p = kzalloc(sizeof(struct ps3_storage_device) +
427 num_regions * sizeof(struct ps3_storage_region),
434 p->sbd.match_id = match_id;
435 p->sbd.dev_type = PS3_DEVICE_TYPE_SB;
436 p->sbd.bus_id = repo->bus_id;
437 p->sbd.dev_id = repo->dev_id;
438 p->sbd.d_region = &p->dma_region;
439 p->blk_size = blk_size;
440 p->num_regions = num_regions;
442 result = ps3_repository_find_interrupt(repo,
443 PS3_INTERRUPT_TYPE_EVENT_PORT,
444 &p->sbd.interrupt_id);
446 printk(KERN_ERR "%s:%u: find_interrupt failed %d\n", __func__,
449 goto fail_find_interrupt;
452 /* FIXME: Arrange to only do this on a 'cold' boot */
454 result = ps3_storage_wait_for_device(repo);
456 printk(KERN_ERR "%s:%u: storage_notification failed %d\n",
457 __func__, __LINE__, result);
459 goto fail_probe_notification;
462 for (i = 0; i < num_regions; i++) {
466 result = ps3_repository_read_stor_dev_region(repo->bus_index,
472 "%s:%u: read_stor_dev_region failed %d\n",
473 __func__, __LINE__, result);
475 goto fail_read_region;
477 pr_debug("%s:%u: region %u: id %u start %lu size %lu\n",
478 __func__, __LINE__, i, id, start, size);
480 p->regions[i].id = id;
481 p->regions[i].start = start;
482 p->regions[i].size = size;
485 result = ps3_system_bus_device_register(&p->sbd);
487 pr_debug("%s:%u ps3_system_bus_device_register failed\n",
489 goto fail_device_register;
492 pr_debug(" <- %s:%u\n", __func__, __LINE__);
495 fail_device_register:
497 fail_probe_notification:
501 pr_debug(" <- %s:%u: fail.\n", __func__, __LINE__);
505 static int __init ps3_register_vuart_devices(void)
508 unsigned int port_number;
510 pr_debug(" -> %s:%d\n", __func__, __LINE__);
512 result = ps3_repository_read_vuart_av_port(&port_number);
514 port_number = 0; /* av default */
516 result = ps3_setup_vuart_device(PS3_MATCH_ID_AV_SETTINGS, port_number);
519 result = ps3_repository_read_vuart_sysmgr_port(&port_number);
521 port_number = 2; /* sysmgr default */
523 result = ps3_setup_vuart_device(PS3_MATCH_ID_SYSTEM_MANAGER,
527 pr_debug(" <- %s:%d\n", __func__, __LINE__);
531 static int __init ps3_register_sound_devices(void)
535 struct ps3_system_bus_device dev;
536 struct ps3_dma_region d_region;
537 struct ps3_mmio_region m_region;
540 pr_debug(" -> %s:%d\n", __func__, __LINE__);
542 p = kzalloc(sizeof(*p), GFP_KERNEL);
546 p->dev.match_id = PS3_MATCH_ID_SOUND;
547 p->dev.dev_type = PS3_DEVICE_TYPE_IOC0;
548 p->dev.d_region = &p->d_region;
549 p->dev.m_region = &p->m_region;
551 result = ps3_system_bus_device_register(&p->dev);
554 pr_debug("%s:%d ps3_system_bus_device_register failed\n",
557 pr_debug(" <- %s:%d\n", __func__, __LINE__);
561 static int __init ps3_register_graphics_devices(void)
565 struct ps3_system_bus_device dev;
568 pr_debug(" -> %s:%d\n", __func__, __LINE__);
570 p = kzalloc(sizeof(struct layout), GFP_KERNEL);
575 p->dev.match_id = PS3_MATCH_ID_GRAPHICS;
576 p->dev.dev_type = PS3_DEVICE_TYPE_IOC0;
578 result = ps3_system_bus_device_register(&p->dev);
581 pr_debug("%s:%d ps3_system_bus_device_register failed\n",
584 pr_debug(" <- %s:%d\n", __func__, __LINE__);
589 * ps3_register_repository_device - Register a device from the repositiory info.
593 static int ps3_register_repository_device(
594 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",
624 case PS3_DEV_TYPE_STOR_DISK:
625 result = ps3_setup_storage_dev(repo, PS3_MATCH_ID_STOR_DISK);
627 /* Some devices are not accessable from the Other OS lpar. */
628 if (result == -ENODEV) {
630 pr_debug("%s:%u: not accessable\n", __func__,
635 pr_debug("%s:%u ps3_setup_storage_dev failed\n",
639 case PS3_DEV_TYPE_STOR_ROM:
640 result = ps3_setup_storage_dev(repo, PS3_MATCH_ID_STOR_ROM);
642 pr_debug("%s:%u ps3_setup_storage_dev failed\n",
646 case PS3_DEV_TYPE_STOR_FLASH:
647 result = ps3_setup_storage_dev(repo, PS3_MATCH_ID_STOR_FLASH);
649 pr_debug("%s:%u ps3_setup_storage_dev failed\n",
655 pr_debug("%s:%u: unsupported dev_type %u\n", __func__, __LINE__,
663 * ps3_probe_thread - Background repository probing at system startup.
665 * This implementation only supports background probing on a single bus.
668 static int ps3_probe_thread(void *data)
670 struct ps3_repository_device *repo = data;
672 unsigned int ms = 250;
674 pr_debug(" -> %s:%u: kthread started\n", __func__, __LINE__);
679 pr_debug("%s:%u: probing...\n", __func__, __LINE__);
682 result = ps3_repository_find_device(repo);
684 if (result == -ENODEV)
685 pr_debug("%s:%u: nothing new\n", __func__,
688 pr_debug("%s:%u: find device error.\n",
691 pr_debug("%s:%u: found device (%u:%u:%u)\n",
692 __func__, __LINE__, repo->bus_index,
693 repo->dev_index, repo->dev_type);
694 ps3_register_repository_device(repo);
695 ps3_repository_bump_device(repo);
700 pr_debug("%s:%u: ms %u\n", __func__, __LINE__, ms);
705 msleep_interruptible(ms);
707 /* An exponential backoff. */
710 } while (!kthread_should_stop());
712 pr_debug(" <- %s:%u: kthread finished\n", __func__, __LINE__);
718 * ps3_start_probe_thread - Starts the background probe thread.
722 static int __init ps3_start_probe_thread(enum ps3_bus_type bus_type)
725 struct task_struct *task;
726 static struct ps3_repository_device repo; /* must be static */
728 pr_debug(" -> %s:%d\n", __func__, __LINE__);
730 memset(&repo, 0, sizeof(repo));
732 repo.bus_type = bus_type;
734 result = ps3_repository_find_bus(repo.bus_type, 0, &repo.bus_index);
737 printk(KERN_ERR "%s: Cannot find bus (%d)\n", __func__, result);
741 result = ps3_repository_read_bus_id(repo.bus_index, &repo.bus_id);
744 printk(KERN_ERR "%s: read_bus_id failed %d\n", __func__,
749 task = kthread_run(ps3_probe_thread, &repo, "ps3-probe-%u", bus_type);
752 result = PTR_ERR(task);
753 printk(KERN_ERR "%s: kthread_run failed %d\n", __func__,
758 pr_debug(" <- %s:%d\n", __func__, __LINE__);
763 * ps3_register_devices - Probe the system and register devices found.
765 * A device_initcall() routine.
768 static int __init ps3_register_devices(void)
772 if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
775 pr_debug(" -> %s:%d\n", __func__, __LINE__);
777 /* ps3_repository_dump_bus_info(); */
779 result = ps3_start_probe_thread(PS3_BUS_TYPE_STORAGE);
781 ps3_register_vuart_devices();
783 ps3_register_graphics_devices();
785 ps3_repository_find_devices(PS3_BUS_TYPE_SB,
786 ps3_register_repository_device);
788 ps3_register_sound_devices();
790 pr_debug(" <- %s:%d\n", __func__, __LINE__);
794 device_initcall(ps3_register_devices);