Home | History | Annotate | Line # | Download | only in bonito
bonito_iobc.c revision 1.3
      1 /*	$NetBSD: bonito_iobc.c,v 1.3 2005/12/11 12:18:07 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2002 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe.
      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  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Code to manipulate the I/O Buffer Cache on the BONITO.
     41  *
     42  * The BONITO snoops uncached access to memory (e.g. via KSEG1); we only
     43  * need to deal with the IOBC for DMA to cached memory.
     44  *
     45  * Note: This only applies to the 32-bit BONITO; BONITO64's IOBC
     46  * is coherent.
     47  */
     48 
     49 #include <sys/cdefs.h>
     50 __KERNEL_RCSID(0, "$NetBSD: bonito_iobc.c,v 1.3 2005/12/11 12:18:07 christos Exp $");
     51 
     52 #include <sys/param.h>
     53 
     54 #include <machine/locore.h>
     55 #include <machine/intr.h>
     56 
     57 #include <mips/bonito/bonitoreg.h>
     58 #include <mips/bonito/bonitovar.h>
     59 
     60 #define	CACHECMD_INVAL		0
     61 #define	CACHECMD_WBINV		1
     62 #define	CACHECMD_RDTAG		2
     63 #define	CACHECMD_WQFLUSH	3
     64 
     65 #define	IOBC_LINESIZE		32
     66 #define	IOBC_LINESHIFT		5
     67 #define	IOBC_NLINES		4
     68 
     69 #define	TAG_LOCK		0x80000000
     70 #define	TAG_WBACK		0x40000000
     71 #define	TAG_PFPEND		0x20000000
     72 #define	TAG_PEND		0x10000000
     73 #define	TAG_MOD			0x08000000
     74 #define	TAG_PFDVAL		0x04000000
     75 #define	TAG_DVAL		0x02000000
     76 #define	TAG_AVAL		0x01000000
     77 #define	TAG_ADDR		0x00ffffff
     78 
     79 #define	IOBC_LOCK(s)		(s) = splhigh()
     80 #define	IOBC_UNLOCK(s)		splx((s))
     81 
     82 static void
     83 bonito_iobc_cmd(uint32_t cmd, uint32_t line)
     84 {
     85 	uint32_t ctrl;
     86 
     87 	ctrl = (cmd << BONITO_PCICACHECTRL_CACHECMD_SHIFT) |
     88 	       (line << BONITO_PCICACHECTRL_CACHECMDLINE_SHIFT);
     89 
     90 	REGVAL(BONITO_PCICACHECTRL) = ctrl;
     91 	wbflush();
     92 
     93 	REGVAL(BONITO_PCICACHECTRL) = ctrl | BONITO_PCICACHECTRL_CMDEXEC;
     94 	wbflush();
     95 
     96 	while (REGVAL(BONITO_PCICACHECTRL) & BONITO_PCICACHECTRL_CMDEXEC)
     97 		/* spin */ ;
     98 
     99 	REGVAL(BONITO_PCICACHECTRL) = ctrl;
    100 	wbflush();
    101 }
    102 
    103 /*
    104  * bonito_iobc_wbinv_range:
    105  *
    106  *	Write-back and invalidate the specified range in
    107  *	the BONITO IOBC.
    108  */
    109 void
    110 bonito_iobc_wbinv_range(paddr_t pa, psize_t size)
    111 {
    112 	uint32_t line, tag;
    113 	paddr_t tagaddr;
    114 	int s;
    115 
    116 	IOBC_LOCK(s);
    117 
    118 	for (line = 0; line < IOBC_NLINES; line++) {
    119 		bonito_iobc_cmd(CACHECMD_RDTAG, line);
    120 		tag = REGVAL(BONITO_PCICACHETAG);
    121 		if (tag & TAG_AVAL) {
    122 			tagaddr = (tag & TAG_ADDR) << IOBC_LINESHIFT;
    123 			if (tagaddr < (pa + size) &&
    124 			    (tagaddr + IOBC_LINESIZE) > pa)
    125 				bonito_iobc_cmd(CACHECMD_WBINV, line);
    126 		}
    127 	}
    128 	bonito_iobc_cmd(CACHECMD_WQFLUSH, 0);
    129 
    130 	IOBC_UNLOCK(s);
    131 }
    132 
    133 /*
    134  * bonito_iobc_inv_range:
    135  *
    136  *	Invalidate the specified range in the BONITO IOBC.
    137  */
    138 void
    139 bonito_iobc_inv_range(paddr_t pa, psize_t size)
    140 {
    141 	uint32_t line, tag;
    142 	paddr_t tagaddr;
    143 	int s;
    144 
    145 	IOBC_LOCK(s);
    146 
    147 	for (line = 0; line < IOBC_NLINES; line++) {
    148 		bonito_iobc_cmd(CACHECMD_RDTAG, line);
    149 		tag = REGVAL(BONITO_PCICACHETAG);
    150 		if (tag & TAG_AVAL) {
    151 			tagaddr = (tag & TAG_ADDR) << IOBC_LINESHIFT;
    152 			if (tagaddr < (pa + size) &&
    153 			    (tagaddr + IOBC_LINESIZE) > pa)
    154 				bonito_iobc_cmd(CACHECMD_INVAL, line);
    155 		}
    156 	}
    157 	bonito_iobc_cmd(CACHECMD_WQFLUSH, 0);
    158 
    159 	IOBC_UNLOCK(s);
    160 }
    161