1 /* $NetBSD: sif.c,v 1.13 2023/11/05 21:54:27 andvar Exp $ */ 2 3 /*- 4 * Copyright (c) 2001 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by UCHIYAMA Yasushi. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: sif.c,v 1.13 2023/11/05 21:54:27 andvar Exp $"); 34 35 #include "debug_playstation2.h" 36 37 #include <sys/intr.h> 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 #include <sys/kernel.h> 41 42 #include <playstation2/playstation2/sifbios.h> 43 #include <playstation2/ee/dmacvar.h> 44 #include <playstation2/ee/sifvar.h> 45 46 #ifdef SIF_DEBUG 47 int __spd_total_alloc; 48 int sif_debug = 1; 49 #define DPRINTF(fmt, args...) \ 50 if (sif_debug) \ 51 printf("%s: " fmt, __func__ , ##args) 52 #define DPRINTFN(n, arg) \ 53 if (sif_debug > (n)) \ 54 n printf("%s: " fmt, __func__ , ##args) 55 #else 56 #define DPRINTF(arg...) ((void)0) 57 #define DPRINTFN(n, arg...) ((void)0) 58 #endif 59 60 #define ROUND64(x) ((((u_int32_t)(x)) + 63) & ~64) 61 #define BCD_TO_DECIMAL(x) \ 62 ({ \ 63 typeof(x) x_ = x; \ 64 (((x_) >> 4) * 10 + ((x_) & 0xf)); \ 65 }) 66 #define MAJOR(x) BCD_TO_DECIMAL(((x) >> 8) & 0xff) 67 #define MINOR(x) BCD_TO_DECIMAL((x) & 0xff) 68 69 void 70 sif_init(void) 71 { 72 u_int32_t vers; 73 74 vers = sifbios_getver(); 75 printf("PlayStation 2 SIF BIOS version %d.%d\n", 76 MAJOR(vers), MINOR(vers)); 77 78 /* Initialize SIF */ 79 if (sifdma_init() < 0) 80 panic("SIFDMA"); 81 if (sifcmd_init() < 0) 82 panic("SIFCMD"); 83 dmac_intr_establish(D_CH5_SIF0, IPL_TTY, sifcmd_intr, 0); 84 if (sifrpc_init() < 0) 85 panic("SIFRPC"); 86 if (iopmem_init() < 0) 87 panic("IOP Memory"); 88 } 89 90 void 91 sif_exit(void) 92 { 93 94 sifrpc_exit(); 95 sifcmd_exit(); 96 dmac_intr_disestablish((void *)D_CH5_SIF0); 97 sifdma_exit(); 98 } 99 100 int 101 iopdma_allocate_buffer(struct iopdma_segment *seg, size_t size) 102 { 103 /* 104 * To avoid cache inconsistecy as the result of DMA(to memory), 105 * DMA buffer size is set to multiple of CPU cache line size (64B) 106 * and aligned to cache line. 107 */ 108 seg->size = ROUND64(size); 109 110 seg->iop_paddr = iopmem_alloc(seg->size); 111 112 if (seg->iop_paddr == 0) { 113 printf("%s: can't allocate IOP memory.\n", __func__); 114 DPRINTF("request = %lu byte, current total = %#x\n", 115 size, __spd_total_alloc); 116 return (1); 117 } 118 119 seg->ee_vaddr = IOPPHYS_TO_EEKV(seg->iop_paddr); 120 121 #ifdef SIF_DEBUG 122 __spd_total_alloc += size; 123 #endif 124 DPRINTF("0x%08x+%#lx (total=%#x)\n", seg->iop_paddr, size, 125 __spd_total_alloc); 126 127 KDASSERT((seg->ee_vaddr & 63) == 0); 128 129 return (0); 130 } 131 132 void 133 iopdma_free_buffer(struct iopdma_segment *seg) 134 { 135 int ret __unused; 136 137 ret = iopmem_free(seg->iop_paddr); 138 #ifdef SIF_DEBUG 139 __spd_total_alloc -= seg->size; 140 #endif 141 DPRINTF("0x%08x (total=%#x, result=%d)\n", seg->iop_paddr, 142 __spd_total_alloc, ret); 143 } 144