blob: ba73b407766885b0fb311c618b055f473a4aacd6 [file] [log] [blame]
David Daney6aa35242008-09-23 00:05:54 -07001/*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
6 * Copyright (C) 2008 David Daney
7 */
8
9#include <linux/sched.h>
10
11#include <asm/processor.h>
12#include <asm/watch.h>
13
14/*
Ralf Baechle70342282013-01-22 12:59:30 +010015 * Install the watch registers for the current thread. A maximum of
David Daney6aa35242008-09-23 00:05:54 -070016 * four registers are installed although the machine may have more.
17 */
James Hogana7e89322016-03-01 22:19:36 +000018void mips_install_watch_registers(struct task_struct *t)
David Daney6aa35242008-09-23 00:05:54 -070019{
James Hogana7e89322016-03-01 22:19:36 +000020 struct mips3264_watch_reg_state *watches = &t->thread.watch.mips3264;
Matt Redfearnf609cc32018-01-02 11:31:21 +000021 unsigned int watchhi = MIPS_WATCHHI_G | /* Trap all ASIDs */
22 MIPS_WATCHHI_IRW; /* Clear result bits */
23
David Daney6aa35242008-09-23 00:05:54 -070024 switch (current_cpu_data.watch_reg_use_cnt) {
25 default:
26 BUG();
27 case 4:
28 write_c0_watchlo3(watches->watchlo[3]);
Matt Redfearnf609cc32018-01-02 11:31:21 +000029 write_c0_watchhi3(watchhi | watches->watchhi[3]);
Mathieu Malaterre69095e32018-12-03 22:23:43 +010030 /* fall through */
David Daney6aa35242008-09-23 00:05:54 -070031 case 3:
32 write_c0_watchlo2(watches->watchlo[2]);
Matt Redfearnf609cc32018-01-02 11:31:21 +000033 write_c0_watchhi2(watchhi | watches->watchhi[2]);
Mathieu Malaterre69095e32018-12-03 22:23:43 +010034 /* fall through */
David Daney6aa35242008-09-23 00:05:54 -070035 case 2:
36 write_c0_watchlo1(watches->watchlo[1]);
Matt Redfearnf609cc32018-01-02 11:31:21 +000037 write_c0_watchhi1(watchhi | watches->watchhi[1]);
Mathieu Malaterre69095e32018-12-03 22:23:43 +010038 /* fall through */
David Daney6aa35242008-09-23 00:05:54 -070039 case 1:
40 write_c0_watchlo0(watches->watchlo[0]);
Matt Redfearnf609cc32018-01-02 11:31:21 +000041 write_c0_watchhi0(watchhi | watches->watchhi[0]);
David Daney6aa35242008-09-23 00:05:54 -070042 }
43}
44
45/*
46 * Read back the watchhi registers so the user space debugger has
47 * access to the I, R, and W bits. A maximum of four registers are
48 * read although the machine may have more.
49 */
50void mips_read_watch_registers(void)
51{
52 struct mips3264_watch_reg_state *watches =
53 &current->thread.watch.mips3264;
Matt Redfearn705e71a2018-01-02 11:31:22 +000054 unsigned int watchhi_mask = MIPS_WATCHHI_MASK | MIPS_WATCHHI_IRW;
55
David Daney6aa35242008-09-23 00:05:54 -070056 switch (current_cpu_data.watch_reg_use_cnt) {
57 default:
58 BUG();
59 case 4:
Matt Redfearn705e71a2018-01-02 11:31:22 +000060 watches->watchhi[3] = (read_c0_watchhi3() & watchhi_mask);
Mathieu Malaterre69095e32018-12-03 22:23:43 +010061 /* fall through */
David Daney6aa35242008-09-23 00:05:54 -070062 case 3:
Matt Redfearn705e71a2018-01-02 11:31:22 +000063 watches->watchhi[2] = (read_c0_watchhi2() & watchhi_mask);
Mathieu Malaterre69095e32018-12-03 22:23:43 +010064 /* fall through */
David Daney6aa35242008-09-23 00:05:54 -070065 case 2:
Matt Redfearn705e71a2018-01-02 11:31:22 +000066 watches->watchhi[1] = (read_c0_watchhi1() & watchhi_mask);
Mathieu Malaterre69095e32018-12-03 22:23:43 +010067 /* fall through */
David Daney6aa35242008-09-23 00:05:54 -070068 case 1:
Matt Redfearn705e71a2018-01-02 11:31:22 +000069 watches->watchhi[0] = (read_c0_watchhi0() & watchhi_mask);
David Daney6aa35242008-09-23 00:05:54 -070070 }
71 if (current_cpu_data.watch_reg_use_cnt == 1 &&
James Hogan50af5012016-03-01 22:19:39 +000072 (watches->watchhi[0] & MIPS_WATCHHI_IRW) == 0) {
David Daney6aa35242008-09-23 00:05:54 -070073 /* Pathological case of release 1 architecture that
74 * doesn't set the condition bits. We assume that
75 * since we got here, the watch condition was met and
76 * signal that the conditions requested in watchlo
77 * were met. */
James Hogan50af5012016-03-01 22:19:39 +000078 watches->watchhi[0] |= (watches->watchlo[0] & MIPS_WATCHHI_IRW);
David Daney6aa35242008-09-23 00:05:54 -070079 }
80 }
81
82/*
Ralf Baechle70342282013-01-22 12:59:30 +010083 * Disable all watch registers. Although only four registers are
David Daney6aa35242008-09-23 00:05:54 -070084 * installed, all are cleared to eliminate the possibility of endless
85 * looping in the watch handler.
86 */
87void mips_clear_watch_registers(void)
88{
89 switch (current_cpu_data.watch_reg_count) {
90 default:
91 BUG();
92 case 8:
93 write_c0_watchlo7(0);
Mathieu Malaterre69095e32018-12-03 22:23:43 +010094 /* fall through */
David Daney6aa35242008-09-23 00:05:54 -070095 case 7:
96 write_c0_watchlo6(0);
Mathieu Malaterre69095e32018-12-03 22:23:43 +010097 /* fall through */
David Daney6aa35242008-09-23 00:05:54 -070098 case 6:
99 write_c0_watchlo5(0);
Mathieu Malaterre69095e32018-12-03 22:23:43 +0100100 /* fall through */
David Daney6aa35242008-09-23 00:05:54 -0700101 case 5:
102 write_c0_watchlo4(0);
Mathieu Malaterre69095e32018-12-03 22:23:43 +0100103 /* fall through */
David Daney6aa35242008-09-23 00:05:54 -0700104 case 4:
105 write_c0_watchlo3(0);
Mathieu Malaterre69095e32018-12-03 22:23:43 +0100106 /* fall through */
David Daney6aa35242008-09-23 00:05:54 -0700107 case 3:
108 write_c0_watchlo2(0);
Mathieu Malaterre69095e32018-12-03 22:23:43 +0100109 /* fall through */
David Daney6aa35242008-09-23 00:05:54 -0700110 case 2:
111 write_c0_watchlo1(0);
Mathieu Malaterre69095e32018-12-03 22:23:43 +0100112 /* fall through */
David Daney6aa35242008-09-23 00:05:54 -0700113 case 1:
114 write_c0_watchlo0(0);
115 }
116}
117
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000118void mips_probe_watch_registers(struct cpuinfo_mips *c)
David Daney6aa35242008-09-23 00:05:54 -0700119{
120 unsigned int t;
121
122 if ((c->options & MIPS_CPU_WATCH) == 0)
123 return;
124 /*
125 * Check which of the I,R and W bits are supported, then
126 * disable the register.
127 */
James Hogan50af5012016-03-01 22:19:39 +0000128 write_c0_watchlo0(MIPS_WATCHLO_IRW);
Paul Burtonc5e15032013-06-15 15:34:40 +0000129 back_to_back_c0_hazard();
David Daney6aa35242008-09-23 00:05:54 -0700130 t = read_c0_watchlo0();
131 write_c0_watchlo0(0);
James Hogan50af5012016-03-01 22:19:39 +0000132 c->watch_reg_masks[0] = t & MIPS_WATCHLO_IRW;
David Daney6aa35242008-09-23 00:05:54 -0700133
134 /* Write the mask bits and read them back to determine which
135 * can be used. */
136 c->watch_reg_count = 1;
137 c->watch_reg_use_cnt = 1;
138 t = read_c0_watchhi0();
James Hogan50af5012016-03-01 22:19:39 +0000139 write_c0_watchhi0(t | MIPS_WATCHHI_MASK);
Paul Burtonc5e15032013-06-15 15:34:40 +0000140 back_to_back_c0_hazard();
David Daney6aa35242008-09-23 00:05:54 -0700141 t = read_c0_watchhi0();
James Hogan50af5012016-03-01 22:19:39 +0000142 c->watch_reg_masks[0] |= (t & MIPS_WATCHHI_MASK);
143 if ((t & MIPS_WATCHHI_M) == 0)
David Daney6aa35242008-09-23 00:05:54 -0700144 return;
145
James Hogan50af5012016-03-01 22:19:39 +0000146 write_c0_watchlo1(MIPS_WATCHLO_IRW);
Paul Burtonc5e15032013-06-15 15:34:40 +0000147 back_to_back_c0_hazard();
David Daney6aa35242008-09-23 00:05:54 -0700148 t = read_c0_watchlo1();
149 write_c0_watchlo1(0);
James Hogan50af5012016-03-01 22:19:39 +0000150 c->watch_reg_masks[1] = t & MIPS_WATCHLO_IRW;
David Daney6aa35242008-09-23 00:05:54 -0700151
152 c->watch_reg_count = 2;
153 c->watch_reg_use_cnt = 2;
154 t = read_c0_watchhi1();
James Hogan50af5012016-03-01 22:19:39 +0000155 write_c0_watchhi1(t | MIPS_WATCHHI_MASK);
Paul Burtonc5e15032013-06-15 15:34:40 +0000156 back_to_back_c0_hazard();
David Daney6aa35242008-09-23 00:05:54 -0700157 t = read_c0_watchhi1();
James Hogan50af5012016-03-01 22:19:39 +0000158 c->watch_reg_masks[1] |= (t & MIPS_WATCHHI_MASK);
159 if ((t & MIPS_WATCHHI_M) == 0)
David Daney6aa35242008-09-23 00:05:54 -0700160 return;
161
James Hogan50af5012016-03-01 22:19:39 +0000162 write_c0_watchlo2(MIPS_WATCHLO_IRW);
Paul Burtonc5e15032013-06-15 15:34:40 +0000163 back_to_back_c0_hazard();
David Daney6aa35242008-09-23 00:05:54 -0700164 t = read_c0_watchlo2();
165 write_c0_watchlo2(0);
James Hogan50af5012016-03-01 22:19:39 +0000166 c->watch_reg_masks[2] = t & MIPS_WATCHLO_IRW;
David Daney6aa35242008-09-23 00:05:54 -0700167
168 c->watch_reg_count = 3;
169 c->watch_reg_use_cnt = 3;
170 t = read_c0_watchhi2();
James Hogan50af5012016-03-01 22:19:39 +0000171 write_c0_watchhi2(t | MIPS_WATCHHI_MASK);
Paul Burtonc5e15032013-06-15 15:34:40 +0000172 back_to_back_c0_hazard();
David Daney6aa35242008-09-23 00:05:54 -0700173 t = read_c0_watchhi2();
James Hogan50af5012016-03-01 22:19:39 +0000174 c->watch_reg_masks[2] |= (t & MIPS_WATCHHI_MASK);
175 if ((t & MIPS_WATCHHI_M) == 0)
David Daney6aa35242008-09-23 00:05:54 -0700176 return;
177
James Hogan50af5012016-03-01 22:19:39 +0000178 write_c0_watchlo3(MIPS_WATCHLO_IRW);
Paul Burtonc5e15032013-06-15 15:34:40 +0000179 back_to_back_c0_hazard();
David Daney6aa35242008-09-23 00:05:54 -0700180 t = read_c0_watchlo3();
181 write_c0_watchlo3(0);
James Hogan50af5012016-03-01 22:19:39 +0000182 c->watch_reg_masks[3] = t & MIPS_WATCHLO_IRW;
David Daney6aa35242008-09-23 00:05:54 -0700183
184 c->watch_reg_count = 4;
185 c->watch_reg_use_cnt = 4;
186 t = read_c0_watchhi3();
James Hogan50af5012016-03-01 22:19:39 +0000187 write_c0_watchhi3(t | MIPS_WATCHHI_MASK);
Paul Burtonc5e15032013-06-15 15:34:40 +0000188 back_to_back_c0_hazard();
David Daney6aa35242008-09-23 00:05:54 -0700189 t = read_c0_watchhi3();
James Hogan50af5012016-03-01 22:19:39 +0000190 c->watch_reg_masks[3] |= (t & MIPS_WATCHHI_MASK);
191 if ((t & MIPS_WATCHHI_M) == 0)
David Daney6aa35242008-09-23 00:05:54 -0700192 return;
193
194 /* We use at most 4, but probe and report up to 8. */
195 c->watch_reg_count = 5;
196 t = read_c0_watchhi4();
James Hogan50af5012016-03-01 22:19:39 +0000197 if ((t & MIPS_WATCHHI_M) == 0)
David Daney6aa35242008-09-23 00:05:54 -0700198 return;
199
200 c->watch_reg_count = 6;
201 t = read_c0_watchhi5();
James Hogan50af5012016-03-01 22:19:39 +0000202 if ((t & MIPS_WATCHHI_M) == 0)
David Daney6aa35242008-09-23 00:05:54 -0700203 return;
204
205 c->watch_reg_count = 7;
206 t = read_c0_watchhi6();
James Hogan50af5012016-03-01 22:19:39 +0000207 if ((t & MIPS_WATCHHI_M) == 0)
David Daney6aa35242008-09-23 00:05:54 -0700208 return;
209
210 c->watch_reg_count = 8;
211}