1/*	$NetBSD: bonito_iobc.c,v 1.4 2008/04/28 20:23:28 martin 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 *
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/*
33 * Code to manipulate the I/O Buffer Cache on the BONITO.
34 *
35 * The BONITO snoops uncached access to memory (e.g. via KSEG1); we only
36 * need to deal with the IOBC for DMA to cached memory.
37 *
38 * Note: This only applies to the 32-bit BONITO; BONITO64's IOBC
39 * is coherent.
40 */
41
42#include <sys/cdefs.h>
43__KERNEL_RCSID(0, "$NetBSD: bonito_iobc.c,v 1.4 2008/04/28 20:23:28 martin Exp $");
44
45#include <sys/param.h>
46#include <sys/intr.h>
47
48#include <mips/locore.h>
49
50#include <mips/bonito/bonitoreg.h>
51#include <mips/bonito/bonitovar.h>
52
53#define	CACHECMD_INVAL		0
54#define	CACHECMD_WBINV		1
55#define	CACHECMD_RDTAG		2
56#define	CACHECMD_WQFLUSH	3
57
58#define	IOBC_LINESIZE		32
59#define	IOBC_LINESHIFT		5
60#define	IOBC_NLINES		4
61
62#define	TAG_LOCK		0x80000000
63#define	TAG_WBACK		0x40000000
64#define	TAG_PFPEND		0x20000000
65#define	TAG_PEND		0x10000000
66#define	TAG_MOD			0x08000000
67#define	TAG_PFDVAL		0x04000000
68#define	TAG_DVAL		0x02000000
69#define	TAG_AVAL		0x01000000
70#define	TAG_ADDR		0x00ffffff
71
72#define	IOBC_LOCK(s)		(s) = splhigh()
73#define	IOBC_UNLOCK(s)		splx((s))
74
75static void
76bonito_iobc_cmd(uint32_t cmd, uint32_t line)
77{
78	uint32_t ctrl;
79
80	ctrl = (cmd << BONITO_PCICACHECTRL_CACHECMD_SHIFT) |
81	       (line << BONITO_PCICACHECTRL_CACHECMDLINE_SHIFT);
82
83	REGVAL(BONITO_PCICACHECTRL) = ctrl;
84	wbflush();
85
86	REGVAL(BONITO_PCICACHECTRL) = ctrl | BONITO_PCICACHECTRL_CMDEXEC;
87	wbflush();
88
89	while (REGVAL(BONITO_PCICACHECTRL) & BONITO_PCICACHECTRL_CMDEXEC)
90		/* spin */ ;
91
92	REGVAL(BONITO_PCICACHECTRL) = ctrl;
93	wbflush();
94}
95
96/*
97 * bonito_iobc_wbinv_range:
98 *
99 *	Write-back and invalidate the specified range in
100 *	the BONITO IOBC.
101 */
102void
103bonito_iobc_wbinv_range(paddr_t pa, psize_t size)
104{
105	uint32_t line, tag;
106	paddr_t tagaddr;
107	int s;
108
109	IOBC_LOCK(s);
110
111	for (line = 0; line < IOBC_NLINES; line++) {
112		bonito_iobc_cmd(CACHECMD_RDTAG, line);
113		tag = REGVAL(BONITO_PCICACHETAG);
114		if (tag & TAG_AVAL) {
115			tagaddr = (tag & TAG_ADDR) << IOBC_LINESHIFT;
116			if (tagaddr < (pa + size) &&
117			    (tagaddr + IOBC_LINESIZE) > pa)
118				bonito_iobc_cmd(CACHECMD_WBINV, line);
119		}
120	}
121	bonito_iobc_cmd(CACHECMD_WQFLUSH, 0);
122
123	IOBC_UNLOCK(s);
124}
125
126/*
127 * bonito_iobc_inv_range:
128 *
129 *	Invalidate the specified range in the BONITO IOBC.
130 */
131void
132bonito_iobc_inv_range(paddr_t pa, psize_t size)
133{
134	uint32_t line, tag;
135	paddr_t tagaddr;
136	int s;
137
138	IOBC_LOCK(s);
139
140	for (line = 0; line < IOBC_NLINES; line++) {
141		bonito_iobc_cmd(CACHECMD_RDTAG, line);
142		tag = REGVAL(BONITO_PCICACHETAG);
143		if (tag & TAG_AVAL) {
144			tagaddr = (tag & TAG_ADDR) << IOBC_LINESHIFT;
145			if (tagaddr < (pa + size) &&
146			    (tagaddr + IOBC_LINESIZE) > pa)
147				bonito_iobc_cmd(CACHECMD_INVAL, line);
148		}
149	}
150	bonito_iobc_cmd(CACHECMD_WQFLUSH, 0);
151
152	IOBC_UNLOCK(s);
153}
154