9dcf5d8966838e5cc605aff8f273083b322d5f45
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / misc / sgi-gru / gruhandles.c
1 /*
2  *              GRU KERNEL MCS INSTRUCTIONS
3  *
4  *  Copyright (c) 2008 Silicon Graphics, Inc.  All Rights Reserved.
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19  */
20
21 #include <linux/kernel.h>
22 #include "gru.h"
23 #include "grulib.h"
24 #include "grutables.h"
25
26 /* 10 sec */
27 #ifdef CONFIG_IA64
28 #include <asm/processor.h>
29 #define GRU_OPERATION_TIMEOUT   (((cycles_t) local_cpu_data->itc_freq)*10)
30 #else
31 #include <asm/tsc.h>
32 #define GRU_OPERATION_TIMEOUT   ((cycles_t) tsc_khz*10*1000)
33 #endif
34
35 /* Extract the status field from a kernel handle */
36 #define GET_MSEG_HANDLE_STATUS(h)       (((*(unsigned long *)(h)) >> 16) & 3)
37
38 struct mcs_op_statistic mcs_op_statistics[mcsop_last];
39
40 static void update_mcs_stats(enum mcs_op op, unsigned long clks)
41 {
42         atomic_long_inc(&mcs_op_statistics[op].count);
43         atomic_long_add(clks, &mcs_op_statistics[op].total);
44         if (mcs_op_statistics[op].max < clks)
45                 mcs_op_statistics[op].max = clks;
46 }
47
48 static void start_instruction(void *h)
49 {
50         unsigned long *w0 = h;
51
52         wmb();          /* setting CMD bit must be last */
53         *w0 = *w0 | 1;
54         gru_flush_cache(h);
55 }
56
57 static int wait_instruction_complete(void *h, enum mcs_op opc)
58 {
59         int status;
60         cycles_t start_time = get_cycles();
61
62         while (1) {
63                 cpu_relax();
64                 status = GET_MSEG_HANDLE_STATUS(h);
65                 if (status != CCHSTATUS_ACTIVE)
66                         break;
67                 if (GRU_OPERATION_TIMEOUT < (get_cycles() - start_time))
68                         panic("GRU %p is malfunctioning\n", h);
69         }
70         if (gru_options & OPT_STATS)
71                 update_mcs_stats(opc, get_cycles() - start_time);
72         return status;
73 }
74
75 #if defined CONFIG_IA64
76 static void cch_allocate_set_asids(
77                   struct gru_context_configuration_handle *cch, int asidval)
78 {
79         int i;
80
81         for (i = 0; i < 8; i++) {
82                 cch->asid[i] = (asidval++);
83 #if 0
84                 /* ZZZ hugepages not supported yet */
85                 if (i == RGN_HPAGE)
86                         cch->sizeavail[i] = GRU_SIZEAVAIL(hpage_shift);
87                 else
88 #endif
89                         cch->sizeavail[i] = GRU_SIZEAVAIL(PAGE_SHIFT);
90         }
91 }
92 #elif defined CONFIG_X86_64
93 static void cch_allocate_set_asids(
94                   struct gru_context_configuration_handle *cch, int asidval)
95 {
96         int i;
97
98         for (i = 0; i < 8; i++) {
99                 cch->asid[i] = asidval++;
100                 cch->sizeavail[i] = GRU_SIZEAVAIL(PAGE_SHIFT) |
101                         GRU_SIZEAVAIL(21);
102         }
103 }
104 #endif
105
106 int cch_allocate(struct gru_context_configuration_handle *cch,
107                                int asidval, unsigned long cbrmap,
108                                unsigned long dsrmap)
109 {
110         cch_allocate_set_asids(cch, asidval);
111         cch->dsr_allocation_map = dsrmap;
112         cch->cbr_allocation_map = cbrmap;
113         cch->opc = CCHOP_ALLOCATE;
114         start_instruction(cch);
115         return wait_instruction_complete(cch, cchop_allocate);
116 }
117
118 int cch_start(struct gru_context_configuration_handle *cch)
119 {
120         cch->opc = CCHOP_START;
121         start_instruction(cch);
122         return wait_instruction_complete(cch, cchop_start);
123 }
124
125 int cch_interrupt(struct gru_context_configuration_handle *cch)
126 {
127         cch->opc = CCHOP_INTERRUPT;
128         start_instruction(cch);
129         return wait_instruction_complete(cch, cchop_interrupt);
130 }
131
132 int cch_deallocate(struct gru_context_configuration_handle *cch)
133 {
134         cch->opc = CCHOP_DEALLOCATE;
135         start_instruction(cch);
136         return wait_instruction_complete(cch, cchop_deallocate);
137 }
138
139 int cch_interrupt_sync(struct gru_context_configuration_handle
140                                      *cch)
141 {
142         cch->opc = CCHOP_INTERRUPT_SYNC;
143         start_instruction(cch);
144         return wait_instruction_complete(cch, cchop_interrupt_sync);
145 }
146
147 int tgh_invalidate(struct gru_tlb_global_handle *tgh,
148                                  unsigned long vaddr, unsigned long vaddrmask,
149                                  int asid, int pagesize, int global, int n,
150                                  unsigned short ctxbitmap)
151 {
152         tgh->vaddr = vaddr;
153         tgh->asid = asid;
154         tgh->pagesize = pagesize;
155         tgh->n = n;
156         tgh->global = global;
157         tgh->vaddrmask = vaddrmask;
158         tgh->ctxbitmap = ctxbitmap;
159         tgh->opc = TGHOP_TLBINV;
160         start_instruction(tgh);
161         return wait_instruction_complete(tgh, tghop_invalidate);
162 }
163
164 void tfh_write_only(struct gru_tlb_fault_handle *tfh,
165                                   unsigned long pfn, unsigned long vaddr,
166                                   int asid, int dirty, int pagesize)
167 {
168         tfh->fillasid = asid;
169         tfh->fillvaddr = vaddr;
170         tfh->pfn = pfn;
171         tfh->dirty = dirty;
172         tfh->pagesize = pagesize;
173         tfh->opc = TFHOP_WRITE_ONLY;
174         start_instruction(tfh);
175 }
176
177 void tfh_write_restart(struct gru_tlb_fault_handle *tfh,
178                                      unsigned long paddr, int gaa,
179                                      unsigned long vaddr, int asid, int dirty,
180                                      int pagesize)
181 {
182         tfh->fillasid = asid;
183         tfh->fillvaddr = vaddr;
184         tfh->pfn = paddr >> GRU_PADDR_SHIFT;
185         tfh->gaa = gaa;
186         tfh->dirty = dirty;
187         tfh->pagesize = pagesize;
188         tfh->opc = TFHOP_WRITE_RESTART;
189         start_instruction(tfh);
190 }
191
192 void tfh_restart(struct gru_tlb_fault_handle *tfh)
193 {
194         tfh->opc = TFHOP_RESTART;
195         start_instruction(tfh);
196 }
197
198 void tfh_user_polling_mode(struct gru_tlb_fault_handle *tfh)
199 {
200         tfh->opc = TFHOP_USER_POLLING_MODE;
201         start_instruction(tfh);
202 }
203
204 void tfh_exception(struct gru_tlb_fault_handle *tfh)
205 {
206         tfh->opc = TFHOP_EXCEPTION;
207         start_instruction(tfh);
208 }
209