2 * Copyright (C) 2010 Nathan Whitehorn
3 * Copyright (C) 2011 glevand (geoffrey.levand@mail.ru)
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/malloc.h>
36 #include <sys/clock.h>
38 #include <sys/resource.h>
44 #include <machine/bus.h>
45 #include <machine/platform.h>
46 #include <machine/pmap.h>
47 #include <machine/resource.h>
50 #include "ps3-hvcall.h"
54 static void ps3bus_identify(driver_t *, device_t);
55 static int ps3bus_probe(device_t);
56 static int ps3bus_attach(device_t);
57 static int ps3bus_print_child(device_t dev, device_t child);
58 static int ps3bus_read_ivar(device_t bus, device_t child, int which,
60 static struct resource *ps3bus_alloc_resource(device_t bus, device_t child,
61 int type, int *rid, u_long start, u_long end,
62 u_long count, u_int flags);
63 static int ps3bus_activate_resource(device_t bus, device_t child, int type,
64 int rid, struct resource *res);
65 static bus_dma_tag_t ps3bus_get_dma_tag(device_t dev, device_t child);
66 static int ps3_iommu_map(device_t dev, bus_dma_segment_t *segs, int *nsegs, bus_addr_t min, bus_addr_t max, bus_size_t alignment,
67 bus_addr_t boundary, void *cookie);
68 static int ps3_iommu_unmap(device_t dev, bus_dma_segment_t *segs,
69 int nsegs, void *cookie);
70 static int ps3_gettime(device_t dev, struct timespec *ts);
71 static int ps3_settime(device_t dev, struct timespec *ts);
73 struct ps3bus_devinfo {
81 struct resource_list resources;
82 bus_dma_tag_t dma_tag;
85 bus_addr_t dma_base[4];
88 static MALLOC_DEFINE(M_PS3BUS, "ps3bus", "PS3 system bus device information");
90 enum ps3bus_irq_type {
96 enum ps3bus_reg_type {
101 static device_method_t ps3bus_methods[] = {
102 /* Device interface */
103 DEVMETHOD(device_identify, ps3bus_identify),
104 DEVMETHOD(device_probe, ps3bus_probe),
105 DEVMETHOD(device_attach, ps3bus_attach),
108 DEVMETHOD(bus_add_child, bus_generic_add_child),
109 DEVMETHOD(bus_get_dma_tag, ps3bus_get_dma_tag),
110 DEVMETHOD(bus_print_child, ps3bus_print_child),
111 DEVMETHOD(bus_read_ivar, ps3bus_read_ivar),
112 DEVMETHOD(bus_alloc_resource, ps3bus_alloc_resource),
113 DEVMETHOD(bus_activate_resource, ps3bus_activate_resource),
114 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
115 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
117 /* IOMMU interface */
118 DEVMETHOD(iommu_map, ps3_iommu_map),
119 DEVMETHOD(iommu_unmap, ps3_iommu_unmap),
121 /* Clock interface */
122 DEVMETHOD(clock_gettime, ps3_gettime),
123 DEVMETHOD(clock_settime, ps3_settime),
128 struct ps3bus_softc {
129 struct rman sc_mem_rman;
130 struct mem_region *regions;
134 static driver_t ps3bus_driver = {
137 sizeof(struct ps3bus_softc)
140 static devclass_t ps3bus_devclass;
142 DRIVER_MODULE(ps3bus, nexus, ps3bus_driver, ps3bus_devclass, 0, 0);
145 ps3bus_identify(driver_t *driver, device_t parent)
147 if (strcmp(installed_platform(), "ps3") != 0)
150 if (device_find_child(parent, "ps3bus", -1) == NULL)
151 BUS_ADD_CHILD(parent, 0, "ps3bus", 0);
155 ps3bus_probe(device_t dev)
157 /* Do not attach to any OF nodes that may be present */
159 device_set_desc(dev, "Playstation 3 System Bus");
161 return (BUS_PROBE_NOWILDCARD);
165 ps3bus_resources_init(struct rman *rm, int bus_index, int dev_index,
166 struct ps3bus_devinfo *dinfo)
168 uint64_t irq_type, irq, outlet;
169 uint64_t reg_type, paddr, len;
174 resource_list_init(&dinfo->resources);
176 lv1_get_logical_ppe_id(&ppe);
177 thread = 32 - fls(mfctrl());
179 /* Scan for interrupts */
180 for (i = 0; i < 10; i++) {
181 result = lv1_get_repository_node_value(PS3_LPAR_ID_PME,
182 (lv1_repository_string("bus") >> 32) | bus_index,
183 lv1_repository_string("dev") | dev_index,
184 lv1_repository_string("intr") | i, 0, &irq_type, &irq);
191 lv1_construct_event_receive_port(&outlet);
192 lv1_connect_irq_plug_ext(ppe, thread, outlet, outlet,
194 lv1_connect_interrupt_event_receive_port(dinfo->bus,
195 dinfo->dev, outlet, irq);
199 lv1_construct_io_irq_outlet(irq, &outlet);
200 lv1_connect_irq_plug_ext(ppe, thread, outlet, outlet,
204 printf("Unknown IRQ type %ld for device %d.%d\n",
205 irq_type, dinfo->bus, dinfo->dev);
209 resource_list_add(&dinfo->resources, SYS_RES_IRQ, i,
213 /* Scan for registers */
214 for (i = 0; i < 10; i++) {
215 result = lv1_get_repository_node_value(PS3_LPAR_ID_PME,
216 (lv1_repository_string("bus") >> 32) | bus_index,
217 lv1_repository_string("dev") | dev_index,
218 lv1_repository_string("reg") | i,
219 lv1_repository_string("type"), ®_type, &junk);
224 result = lv1_get_repository_node_value(PS3_LPAR_ID_PME,
225 (lv1_repository_string("bus") >> 32) | bus_index,
226 lv1_repository_string("dev") | dev_index,
227 lv1_repository_string("reg") | i,
228 lv1_repository_string("data"), &paddr, &len);
230 result = lv1_map_device_mmio_region(dinfo->bus, dinfo->dev,
231 paddr, len, 12 /* log_2(4 KB) */, &paddr);
234 printf("Mapping registers failed for device "
235 "%d.%d (%ld.%ld): %d\n", dinfo->bus, dinfo->dev,
236 dinfo->bustype, dinfo->devtype, result);
240 rman_manage_region(rm, paddr, paddr + len - 1);
241 resource_list_add(&dinfo->resources, SYS_RES_MEMORY, i,
242 paddr, paddr + len, len);
247 ps3bus_resources_init_by_type(struct rman *rm, int bus_index, int dev_index,
248 uint64_t irq_type, uint64_t reg_type, struct ps3bus_devinfo *dinfo)
250 uint64_t _irq_type, irq, outlet;
251 uint64_t _reg_type, paddr, len;
256 resource_list_init(&dinfo->resources);
258 lv1_get_logical_ppe_id(&ppe);
259 thread = 32 - fls(mfctrl());
261 /* Scan for interrupts */
262 for (i = 0; i < 10; i++) {
263 result = lv1_get_repository_node_value(PS3_LPAR_ID_PME,
264 (lv1_repository_string("bus") >> 32) | bus_index,
265 lv1_repository_string("dev") | dev_index,
266 lv1_repository_string("intr") | i, 0, &_irq_type, &irq);
271 if (_irq_type != irq_type)
274 lv1_construct_io_irq_outlet(irq, &outlet);
275 lv1_connect_irq_plug_ext(ppe, thread, outlet, outlet,
277 resource_list_add(&dinfo->resources, SYS_RES_IRQ, i,
281 /* Scan for registers */
282 for (i = 0; i < 10; i++) {
283 result = lv1_get_repository_node_value(PS3_LPAR_ID_PME,
284 (lv1_repository_string("bus") >> 32) | bus_index,
285 lv1_repository_string("dev") | dev_index,
286 lv1_repository_string("reg") | i,
287 lv1_repository_string("type"), &_reg_type, &junk);
292 if (_reg_type != reg_type)
295 result = lv1_get_repository_node_value(PS3_LPAR_ID_PME,
296 (lv1_repository_string("bus") >> 32) | bus_index,
297 lv1_repository_string("dev") | dev_index,
298 lv1_repository_string("reg") | i,
299 lv1_repository_string("data"), &paddr, &len);
301 result = lv1_map_device_mmio_region(dinfo->bus, dinfo->dev,
302 paddr, len, 12 /* log_2(4 KB) */, &paddr);
305 printf("Mapping registers failed for device "
306 "%d.%d (%ld.%ld): %d\n", dinfo->bus, dinfo->dev,
307 dinfo->bustype, dinfo->devtype, result);
311 rman_manage_region(rm, paddr, paddr + len - 1);
312 resource_list_add(&dinfo->resources, SYS_RES_MEMORY, i,
313 paddr, paddr + len, len);
318 ps3bus_attach(device_t self)
320 struct ps3bus_softc *sc;
321 struct ps3bus_devinfo *dinfo;
322 int bus_index, dev_index, result;
323 uint64_t bustype, bus, devs;
324 uint64_t dev, devtype;
328 sc = device_get_softc(self);
329 sc->sc_mem_rman.rm_type = RMAN_ARRAY;
330 sc->sc_mem_rman.rm_descr = "PS3Bus Memory Mapped I/O";
331 rman_init(&sc->sc_mem_rman);
333 /* Get memory regions for DMA */
334 mem_regions(&sc->regions, &sc->rcount, &sc->regions, &sc->rcount);
337 * Probe all the PS3's buses.
340 for (bus_index = 0; bus_index < 5; bus_index++) {
341 result = lv1_get_repository_node_value(PS3_LPAR_ID_PME,
342 (lv1_repository_string("bus") >> 32) | bus_index,
343 lv1_repository_string("type"), 0, 0, &bustype, &junk);
348 result = lv1_get_repository_node_value(PS3_LPAR_ID_PME,
349 (lv1_repository_string("bus") >> 32) | bus_index,
350 lv1_repository_string("id"), 0, 0, &bus, &junk);
355 result = lv1_get_repository_node_value(PS3_LPAR_ID_PME,
356 (lv1_repository_string("bus") >> 32) | bus_index,
357 lv1_repository_string("num_dev"), 0, 0, &devs, &junk);
359 for (dev_index = 0; dev_index < devs; dev_index++) {
360 result = lv1_get_repository_node_value(PS3_LPAR_ID_PME,
361 (lv1_repository_string("bus") >> 32) | bus_index,
362 lv1_repository_string("dev") | dev_index,
363 lv1_repository_string("type"), 0, &devtype, &junk);
368 result = lv1_get_repository_node_value(PS3_LPAR_ID_PME,
369 (lv1_repository_string("bus") >> 32) | bus_index,
370 lv1_repository_string("dev") | dev_index,
371 lv1_repository_string("id"), 0, &dev, &junk);
377 case PS3_DEVTYPE_USB:
378 /* USB device has OHCI and EHCI USB host controllers */
380 lv1_open_device(bus, dev, 0);
382 /* OHCI host controller */
384 dinfo = malloc(sizeof(*dinfo), M_PS3BUS,
389 dinfo->bustype = bustype;
390 dinfo->devtype = devtype;
391 dinfo->busidx = bus_index;
392 dinfo->devidx = dev_index;
394 ps3bus_resources_init_by_type(&sc->sc_mem_rman, bus_index,
395 dev_index, OHCI_IRQ, OHCI_REG, dinfo);
397 cdev = device_add_child(self, "ohci", -1);
400 "device_add_child failed\n");
401 free(dinfo, M_PS3BUS);
405 mtx_init(&dinfo->iommu_mtx, "iommu", NULL, MTX_DEF);
406 device_set_ivars(cdev, dinfo);
408 /* EHCI host controller */
410 dinfo = malloc(sizeof(*dinfo), M_PS3BUS,
415 dinfo->bustype = bustype;
416 dinfo->devtype = devtype;
417 dinfo->busidx = bus_index;
418 dinfo->devidx = dev_index;
420 ps3bus_resources_init_by_type(&sc->sc_mem_rman, bus_index,
421 dev_index, EHCI_IRQ, EHCI_REG, dinfo);
423 cdev = device_add_child(self, "ehci", -1);
426 "device_add_child failed\n");
427 free(dinfo, M_PS3BUS);
431 mtx_init(&dinfo->iommu_mtx, "iommu", NULL, MTX_DEF);
432 device_set_ivars(cdev, dinfo);
435 dinfo = malloc(sizeof(*dinfo), M_PS3BUS,
440 dinfo->bustype = bustype;
441 dinfo->devtype = devtype;
442 dinfo->busidx = bus_index;
443 dinfo->devidx = dev_index;
445 if (dinfo->bustype == PS3_BUSTYPE_SYSBUS ||
446 dinfo->bustype == PS3_BUSTYPE_STORAGE)
447 lv1_open_device(bus, dev, 0);
449 ps3bus_resources_init(&sc->sc_mem_rman, bus_index,
452 cdev = device_add_child(self, NULL, -1);
455 "device_add_child failed\n");
456 free(dinfo, M_PS3BUS);
460 mtx_init(&dinfo->iommu_mtx, "iommu", NULL, MTX_DEF);
461 device_set_ivars(cdev, dinfo);
466 clock_register(self, 1000);
468 return (bus_generic_attach(self));
472 ps3bus_print_child(device_t dev, device_t child)
474 struct ps3bus_devinfo *dinfo = device_get_ivars(child);
477 retval += bus_print_child_header(dev, child);
478 retval += resource_list_print_type(&dinfo->resources, "mem",
479 SYS_RES_MEMORY, "%#lx");
480 retval += resource_list_print_type(&dinfo->resources, "irq",
483 retval += bus_print_child_footer(dev, child);
489 ps3bus_read_ivar(device_t bus, device_t child, int which, uintptr_t *result)
491 struct ps3bus_devinfo *dinfo = device_get_ivars(child);
494 case PS3BUS_IVAR_BUS:
495 *result = dinfo->bus;
497 case PS3BUS_IVAR_DEVICE:
498 *result = dinfo->dev;
500 case PS3BUS_IVAR_BUSTYPE:
501 *result = dinfo->bustype;
503 case PS3BUS_IVAR_DEVTYPE:
504 *result = dinfo->devtype;
506 case PS3BUS_IVAR_BUSIDX:
507 *result = dinfo->busidx;
509 case PS3BUS_IVAR_DEVIDX:
510 *result = dinfo->devidx;
519 static struct resource *
520 ps3bus_alloc_resource(device_t bus, device_t child, int type, int *rid,
521 u_long start, u_long end, u_long count, u_int flags)
523 struct ps3bus_devinfo *dinfo;
524 struct ps3bus_softc *sc;
528 u_long adjstart, adjend, adjcount;
529 struct resource_list_entry *rle;
531 sc = device_get_softc(bus);
532 dinfo = device_get_ivars(child);
533 needactivate = flags & RF_ACTIVE;
538 rle = resource_list_find(&dinfo->resources, SYS_RES_MEMORY,
541 device_printf(bus, "no rle for %s memory %d\n",
542 device_get_nameunit(child), *rid);
546 if (start < rle->start)
547 adjstart = rle->start;
548 else if (start > rle->end)
553 if (end < rle->start)
555 else if (end > rle->end)
560 adjcount = adjend - adjstart;
562 rm = &sc->sc_mem_rman;
565 return (resource_list_alloc(&dinfo->resources, bus, child,
566 type, rid, start, end, count, flags));
568 device_printf(bus, "unknown resource request from %s\n",
569 device_get_nameunit(child));
573 rv = rman_reserve_resource(rm, adjstart, adjend, adjcount, flags,
577 "failed to reserve resource %#lx - %#lx (%#lx)"
578 " for %s\n", adjstart, adjend, adjcount,
579 device_get_nameunit(child));
583 rman_set_rid(rv, *rid);
586 if (bus_activate_resource(child, type, *rid, rv) != 0) {
588 "failed to activate resource for %s\n",
589 device_get_nameunit(child));
590 rman_release_resource(rv);
599 ps3bus_activate_resource(device_t bus, device_t child, int type, int rid,
600 struct resource *res)
604 if (type == SYS_RES_IRQ)
605 return (bus_activate_resource(bus, type, rid, res));
607 if (type == SYS_RES_MEMORY) {
610 start = (vm_offset_t) rman_get_start(res);
613 printf("ps3 mapdev: start %zx, len %ld\n", start,
616 p = pmap_mapdev(start, (vm_size_t) rman_get_size(res));
619 rman_set_virtual(res, p);
620 rman_set_bustag(res, &bs_be_tag);
621 rman_set_bushandle(res, (u_long)p);
624 return (rman_activate_resource(res));
628 ps3bus_get_dma_tag(device_t dev, device_t child)
630 struct ps3bus_devinfo *dinfo = device_get_ivars(child);
631 struct ps3bus_softc *sc = device_get_softc(dev);
632 int i, err, flags, pagesize;
634 if (dinfo->bustype != PS3_BUSTYPE_SYSBUS &&
635 dinfo->bustype != PS3_BUSTYPE_STORAGE)
636 return (bus_get_dma_tag(dev));
638 mtx_lock(&dinfo->iommu_mtx);
639 if (dinfo->dma_tag != NULL) {
640 mtx_unlock(&dinfo->iommu_mtx);
641 return (dinfo->dma_tag);
644 flags = 0; /* 32-bit mode */
645 if (dinfo->bustype == PS3_BUSTYPE_SYSBUS &&
646 dinfo->devtype == PS3_DEVTYPE_USB)
647 flags = 2; /* 8-bit mode */
649 pagesize = 24; /* log_2(16 MB) */
650 if (dinfo->bustype == PS3_BUSTYPE_STORAGE)
651 pagesize = 12; /* 4 KB */
653 for (i = 0; i < sc->rcount; i++) {
654 err = lv1_allocate_device_dma_region(dinfo->bus, dinfo->dev,
655 sc->regions[i].mr_size, pagesize, flags,
656 &dinfo->dma_base[i]);
659 "could not allocate DMA region %d: %d\n", i, err);
663 err = lv1_map_device_dma_region(dinfo->bus, dinfo->dev,
664 sc->regions[i].mr_start, dinfo->dma_base[i],
665 sc->regions[i].mr_size,
666 0xf800000000000800UL /* Cell Handbook Figure 7.3.4.1 */);
669 "could not map DMA region %d: %d\n", i, err);
674 err = bus_dma_tag_create(bus_get_dma_tag(dev),
675 1, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
676 NULL, NULL, BUS_SPACE_MAXSIZE, 0, BUS_SPACE_MAXSIZE,
677 0, NULL, NULL, &dinfo->dma_tag);
680 * Note: storage devices have IOMMU mappings set up by the hypervisor,
681 * but use physical, non-translated addresses. The above IOMMU
682 * initialization is necessary for the hypervisor to be able to set up
683 * the mappings, but actual DMA mappings should not use the IOMMU
686 if (dinfo->bustype != PS3_BUSTYPE_STORAGE)
687 bus_dma_tag_set_iommu(dinfo->dma_tag, dev, dinfo);
690 mtx_unlock(&dinfo->iommu_mtx);
695 return (dinfo->dma_tag);
699 ps3_iommu_map(device_t dev, bus_dma_segment_t *segs, int *nsegs,
700 bus_addr_t min, bus_addr_t max, bus_size_t alignment, bus_addr_t boundary,
703 struct ps3bus_devinfo *dinfo = cookie;
704 struct ps3bus_softc *sc = device_get_softc(dev);
707 for (i = 0; i < *nsegs; i++) {
708 for (j = 0; j < sc->rcount; j++) {
709 if (segs[i].ds_addr >= sc->regions[j].mr_start &&
710 segs[i].ds_addr < sc->regions[j].mr_start +
711 sc->regions[j].mr_size)
714 KASSERT(j < sc->rcount,
715 ("Trying to map address %#lx not in physical memory",
718 segs[i].ds_addr = dinfo->dma_base[j] +
719 (segs[i].ds_addr - sc->regions[j].mr_start);
726 ps3_iommu_unmap(device_t dev, bus_dma_segment_t *segs, int nsegs, void *cookie)
732 #define Y2K 946684800
735 ps3_gettime(device_t dev, struct timespec *ts)
740 result = lv1_get_rtc(&rtc, &tb);
744 ts->tv_sec = rtc + Y2K;
750 ps3_settime(device_t dev, struct timespec *ts)