xref: /openbmc/linux/drivers/misc/sgi-gru/gruhandles.c (revision 67bf04a5c2574e9495f660f418f6df776821d578)
1a24e5e1cSJack Steiner /*
2a24e5e1cSJack Steiner  *              GRU KERNEL MCS INSTRUCTIONS
3a24e5e1cSJack Steiner  *
4a24e5e1cSJack Steiner  *  Copyright (c) 2008 Silicon Graphics, Inc.  All Rights Reserved.
5a24e5e1cSJack Steiner  *
6a24e5e1cSJack Steiner  *  This program is free software; you can redistribute it and/or modify
7a24e5e1cSJack Steiner  *  it under the terms of the GNU General Public License as published by
8a24e5e1cSJack Steiner  *  the Free Software Foundation; either version 2 of the License, or
9a24e5e1cSJack Steiner  *  (at your option) any later version.
10a24e5e1cSJack Steiner  *
11a24e5e1cSJack Steiner  *  This program is distributed in the hope that it will be useful,
12a24e5e1cSJack Steiner  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13a24e5e1cSJack Steiner  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14a24e5e1cSJack Steiner  *  GNU General Public License for more details.
15a24e5e1cSJack Steiner  *
16a24e5e1cSJack Steiner  *  You should have received a copy of the GNU General Public License
17a24e5e1cSJack Steiner  *  along with this program; if not, write to the Free Software
18a24e5e1cSJack Steiner  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19a24e5e1cSJack Steiner  */
20a24e5e1cSJack Steiner 
21a24e5e1cSJack Steiner #include <linux/kernel.h>
22a24e5e1cSJack Steiner #include "gru.h"
23a24e5e1cSJack Steiner #include "grulib.h"
24a24e5e1cSJack Steiner #include "grutables.h"
25a24e5e1cSJack Steiner 
26a24e5e1cSJack Steiner /* 10 sec */
27a24e5e1cSJack Steiner #ifdef CONFIG_IA64
28a24e5e1cSJack Steiner #include <asm/processor.h>
29a24e5e1cSJack Steiner #define GRU_OPERATION_TIMEOUT	(((cycles_t) local_cpu_data->itc_freq)*10)
30a24e5e1cSJack Steiner #else
31a24e5e1cSJack Steiner #include <asm/tsc.h>
32a24e5e1cSJack Steiner #define GRU_OPERATION_TIMEOUT	((cycles_t) tsc_khz*10*1000)
33a24e5e1cSJack Steiner #endif
34a24e5e1cSJack Steiner 
35a24e5e1cSJack Steiner /* Extract the status field from a kernel handle */
36a24e5e1cSJack Steiner #define GET_MSEG_HANDLE_STATUS(h)	(((*(unsigned long *)(h)) >> 16) & 3)
37a24e5e1cSJack Steiner 
38e56484daSJack Steiner struct mcs_op_statistic mcs_op_statistics[mcsop_last];
39e56484daSJack Steiner 
40e56484daSJack Steiner static void update_mcs_stats(enum mcs_op op, unsigned long clks)
41e56484daSJack Steiner {
42e56484daSJack Steiner 	atomic_long_inc(&mcs_op_statistics[op].count);
43e56484daSJack Steiner 	atomic_long_add(clks, &mcs_op_statistics[op].total);
44e56484daSJack Steiner 	if (mcs_op_statistics[op].max < clks)
45e56484daSJack Steiner 		mcs_op_statistics[op].max = clks;
46e56484daSJack Steiner }
47e56484daSJack Steiner 
48a24e5e1cSJack Steiner static void start_instruction(void *h)
49a24e5e1cSJack Steiner {
50a24e5e1cSJack Steiner 	unsigned long *w0 = h;
51a24e5e1cSJack Steiner 
52a24e5e1cSJack Steiner 	wmb();		/* setting CMD bit must be last */
53a24e5e1cSJack Steiner 	*w0 = *w0 | 1;
54a24e5e1cSJack Steiner 	gru_flush_cache(h);
55a24e5e1cSJack Steiner }
56a24e5e1cSJack Steiner 
57648eb8e5SJack Steiner static void report_instruction_timeout(void *h)
58648eb8e5SJack Steiner {
59648eb8e5SJack Steiner 	unsigned long goff = GSEGPOFF((unsigned long)h);
60648eb8e5SJack Steiner 	char *id = "???";
61648eb8e5SJack Steiner 
62648eb8e5SJack Steiner 	if (TYPE_IS(CCH, goff))
63648eb8e5SJack Steiner 		id = "CCH";
64648eb8e5SJack Steiner 	else if (TYPE_IS(TGH, goff))
65648eb8e5SJack Steiner 		id = "TGH";
66648eb8e5SJack Steiner 	else if (TYPE_IS(TFH, goff))
67648eb8e5SJack Steiner 		id = "TFH";
68648eb8e5SJack Steiner 
69648eb8e5SJack Steiner 	panic(KERN_ALERT "GRU %p (%s) is malfunctioning\n", h, id);
70648eb8e5SJack Steiner }
71648eb8e5SJack Steiner 
72a24e5e1cSJack Steiner static int wait_instruction_complete(void *h, enum mcs_op opc)
73a24e5e1cSJack Steiner {
74a24e5e1cSJack Steiner 	int status;
75836ce679SJack Steiner 	unsigned long start_time = get_cycles();
76a24e5e1cSJack Steiner 
77a24e5e1cSJack Steiner 	while (1) {
78a24e5e1cSJack Steiner 		cpu_relax();
79a24e5e1cSJack Steiner 		status = GET_MSEG_HANDLE_STATUS(h);
80a24e5e1cSJack Steiner 		if (status != CCHSTATUS_ACTIVE)
81a24e5e1cSJack Steiner 			break;
82648eb8e5SJack Steiner 		if (GRU_OPERATION_TIMEOUT < (get_cycles() - start_time)) {
83648eb8e5SJack Steiner 			report_instruction_timeout(h);
84648eb8e5SJack Steiner 			start_time = get_cycles();
85648eb8e5SJack Steiner 		}
86a24e5e1cSJack Steiner 	}
87e56484daSJack Steiner 	if (gru_options & OPT_STATS)
88e56484daSJack Steiner 		update_mcs_stats(opc, get_cycles() - start_time);
89a24e5e1cSJack Steiner 	return status;
90a24e5e1cSJack Steiner }
91a24e5e1cSJack Steiner 
926e910074SJack Steiner int cch_allocate(struct gru_context_configuration_handle *cch)
93a24e5e1cSJack Steiner {
94*67bf04a5SJack Steiner 	int ret;
95*67bf04a5SJack Steiner 
96a24e5e1cSJack Steiner 	cch->opc = CCHOP_ALLOCATE;
97a24e5e1cSJack Steiner 	start_instruction(cch);
98*67bf04a5SJack Steiner 	ret = wait_instruction_complete(cch, cchop_allocate);
99*67bf04a5SJack Steiner 
100*67bf04a5SJack Steiner 	/*
101*67bf04a5SJack Steiner 	 * Stop speculation into the GSEG being mapped by the previous ALLOCATE.
102*67bf04a5SJack Steiner 	 * The GSEG memory does not exist until the ALLOCATE completes.
103*67bf04a5SJack Steiner 	 */
104*67bf04a5SJack Steiner 	sync_core();
105*67bf04a5SJack Steiner 	return ret;
106a24e5e1cSJack Steiner }
107a24e5e1cSJack Steiner 
108a24e5e1cSJack Steiner int cch_start(struct gru_context_configuration_handle *cch)
109a24e5e1cSJack Steiner {
110a24e5e1cSJack Steiner 	cch->opc = CCHOP_START;
111a24e5e1cSJack Steiner 	start_instruction(cch);
112a24e5e1cSJack Steiner 	return wait_instruction_complete(cch, cchop_start);
113a24e5e1cSJack Steiner }
114a24e5e1cSJack Steiner 
115a24e5e1cSJack Steiner int cch_interrupt(struct gru_context_configuration_handle *cch)
116a24e5e1cSJack Steiner {
117a24e5e1cSJack Steiner 	cch->opc = CCHOP_INTERRUPT;
118a24e5e1cSJack Steiner 	start_instruction(cch);
119a24e5e1cSJack Steiner 	return wait_instruction_complete(cch, cchop_interrupt);
120a24e5e1cSJack Steiner }
121a24e5e1cSJack Steiner 
122a24e5e1cSJack Steiner int cch_deallocate(struct gru_context_configuration_handle *cch)
123a24e5e1cSJack Steiner {
124*67bf04a5SJack Steiner 	int ret;
125*67bf04a5SJack Steiner 
126a24e5e1cSJack Steiner 	cch->opc = CCHOP_DEALLOCATE;
127a24e5e1cSJack Steiner 	start_instruction(cch);
128*67bf04a5SJack Steiner 	ret = wait_instruction_complete(cch, cchop_deallocate);
129*67bf04a5SJack Steiner 
130*67bf04a5SJack Steiner 	/*
131*67bf04a5SJack Steiner 	 * Stop speculation into the GSEG being unmapped by the previous
132*67bf04a5SJack Steiner 	 * DEALLOCATE.
133*67bf04a5SJack Steiner 	 */
134*67bf04a5SJack Steiner 	sync_core();
135*67bf04a5SJack Steiner 	return ret;
136a24e5e1cSJack Steiner }
137a24e5e1cSJack Steiner 
138a24e5e1cSJack Steiner int cch_interrupt_sync(struct gru_context_configuration_handle
139a24e5e1cSJack Steiner 				     *cch)
140a24e5e1cSJack Steiner {
141a24e5e1cSJack Steiner 	cch->opc = CCHOP_INTERRUPT_SYNC;
142a24e5e1cSJack Steiner 	start_instruction(cch);
143a24e5e1cSJack Steiner 	return wait_instruction_complete(cch, cchop_interrupt_sync);
144a24e5e1cSJack Steiner }
145a24e5e1cSJack Steiner 
146a24e5e1cSJack Steiner int tgh_invalidate(struct gru_tlb_global_handle *tgh,
147a24e5e1cSJack Steiner 				 unsigned long vaddr, unsigned long vaddrmask,
148a24e5e1cSJack Steiner 				 int asid, int pagesize, int global, int n,
149a24e5e1cSJack Steiner 				 unsigned short ctxbitmap)
150a24e5e1cSJack Steiner {
151a24e5e1cSJack Steiner 	tgh->vaddr = vaddr;
152a24e5e1cSJack Steiner 	tgh->asid = asid;
153a24e5e1cSJack Steiner 	tgh->pagesize = pagesize;
154a24e5e1cSJack Steiner 	tgh->n = n;
155a24e5e1cSJack Steiner 	tgh->global = global;
156a24e5e1cSJack Steiner 	tgh->vaddrmask = vaddrmask;
157a24e5e1cSJack Steiner 	tgh->ctxbitmap = ctxbitmap;
158a24e5e1cSJack Steiner 	tgh->opc = TGHOP_TLBINV;
159a24e5e1cSJack Steiner 	start_instruction(tgh);
160a24e5e1cSJack Steiner 	return wait_instruction_complete(tgh, tghop_invalidate);
161a24e5e1cSJack Steiner }
162a24e5e1cSJack Steiner 
163a24e5e1cSJack Steiner void tfh_write_only(struct gru_tlb_fault_handle *tfh,
164a24e5e1cSJack Steiner 				  unsigned long pfn, unsigned long vaddr,
165a24e5e1cSJack Steiner 				  int asid, int dirty, int pagesize)
166a24e5e1cSJack Steiner {
167a24e5e1cSJack Steiner 	tfh->fillasid = asid;
168a24e5e1cSJack Steiner 	tfh->fillvaddr = vaddr;
169a24e5e1cSJack Steiner 	tfh->pfn = pfn;
170a24e5e1cSJack Steiner 	tfh->dirty = dirty;
171a24e5e1cSJack Steiner 	tfh->pagesize = pagesize;
172a24e5e1cSJack Steiner 	tfh->opc = TFHOP_WRITE_ONLY;
173a24e5e1cSJack Steiner 	start_instruction(tfh);
174a24e5e1cSJack Steiner }
175a24e5e1cSJack Steiner 
176a24e5e1cSJack Steiner void tfh_write_restart(struct gru_tlb_fault_handle *tfh,
177a24e5e1cSJack Steiner 				     unsigned long paddr, int gaa,
178a24e5e1cSJack Steiner 				     unsigned long vaddr, int asid, int dirty,
179a24e5e1cSJack Steiner 				     int pagesize)
180a24e5e1cSJack Steiner {
181a24e5e1cSJack Steiner 	tfh->fillasid = asid;
182a24e5e1cSJack Steiner 	tfh->fillvaddr = vaddr;
183a24e5e1cSJack Steiner 	tfh->pfn = paddr >> GRU_PADDR_SHIFT;
184a24e5e1cSJack Steiner 	tfh->gaa = gaa;
185a24e5e1cSJack Steiner 	tfh->dirty = dirty;
186a24e5e1cSJack Steiner 	tfh->pagesize = pagesize;
187a24e5e1cSJack Steiner 	tfh->opc = TFHOP_WRITE_RESTART;
188a24e5e1cSJack Steiner 	start_instruction(tfh);
189a24e5e1cSJack Steiner }
190a24e5e1cSJack Steiner 
191a24e5e1cSJack Steiner void tfh_restart(struct gru_tlb_fault_handle *tfh)
192a24e5e1cSJack Steiner {
193a24e5e1cSJack Steiner 	tfh->opc = TFHOP_RESTART;
194a24e5e1cSJack Steiner 	start_instruction(tfh);
195a24e5e1cSJack Steiner }
196a24e5e1cSJack Steiner 
197a24e5e1cSJack Steiner void tfh_user_polling_mode(struct gru_tlb_fault_handle *tfh)
198a24e5e1cSJack Steiner {
199a24e5e1cSJack Steiner 	tfh->opc = TFHOP_USER_POLLING_MODE;
200a24e5e1cSJack Steiner 	start_instruction(tfh);
201a24e5e1cSJack Steiner }
202a24e5e1cSJack Steiner 
203a24e5e1cSJack Steiner void tfh_exception(struct gru_tlb_fault_handle *tfh)
204a24e5e1cSJack Steiner {
205a24e5e1cSJack Steiner 	tfh->opc = TFHOP_EXCEPTION;
206a24e5e1cSJack Steiner 	start_instruction(tfh);
207a24e5e1cSJack Steiner }
208a24e5e1cSJack Steiner 
209