blob: 57570c0649b46ab7704c1836ecd5ffa8eefac10e [file] [log] [blame]
Thiemo Seufere30ec452008-01-28 20:05:38 +00001/*
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 * A small micro-assembler. It is intentionally kept simple, does only
7 * support a subset of instructions, and does not try to hide pipeline
8 * effects like branch delay slots.
9 *
Ralf Baechle70342282013-01-22 12:59:30 +010010 * Copyright (C) 2004, 2005, 2006, 2008 Thiemo Seufer
Thiemo Seufere30ec452008-01-28 20:05:38 +000011 * Copyright (C) 2005, 2007 Maciej W. Rozycki
12 * Copyright (C) 2006 Ralf Baechle (ralf@linux-mips.org)
Steven J. Hillabc597f2013-02-05 16:52:01 -060013 * Copyright (C) 2012, 2013 MIPS Technologies, Inc. All rights reserved.
Thiemo Seufere30ec452008-01-28 20:05:38 +000014 */
15
Thiemo Seufere30ec452008-01-28 20:05:38 +000016enum fields {
17 RS = 0x001,
18 RT = 0x002,
19 RD = 0x004,
20 RE = 0x008,
21 SIMM = 0x010,
22 UIMM = 0x020,
23 BIMM = 0x040,
24 JIMM = 0x080,
25 FUNC = 0x100,
David Daney58b9e222010-02-18 16:13:03 -080026 SET = 0x200,
Leonid Yegoshin51eec48e2014-11-18 16:24:15 +000027 SCIMM = 0x400,
28 SIMM9 = 0x800,
Thiemo Seufere30ec452008-01-28 20:05:38 +000029};
30
31#define OP_MASK 0x3f
32#define OP_SH 26
Thiemo Seufere30ec452008-01-28 20:05:38 +000033#define RD_MASK 0x1f
34#define RD_SH 11
35#define RE_MASK 0x1f
36#define RE_SH 6
37#define IMM_MASK 0xffff
38#define IMM_SH 0
39#define JIMM_MASK 0x3ffffff
40#define JIMM_SH 0
41#define FUNC_MASK 0x3f
42#define FUNC_SH 0
43#define SET_MASK 0x7
44#define SET_SH 0
Leonid Yegoshin51eec48e2014-11-18 16:24:15 +000045#define SIMM9_SH 7
46#define SIMM9_MASK 0x1ff
Thiemo Seufere30ec452008-01-28 20:05:38 +000047
48enum opcode {
Steven J. Hill71a1c772012-06-19 19:59:29 +010049 insn_addiu, insn_addu, insn_and, insn_andi, insn_bbit0, insn_bbit1,
David Daneydc190122017-06-13 15:28:45 -070050 insn_beq, insn_beql, insn_bgez, insn_bgezl, insn_bgtz, insn_blez,
51 insn_bltz, insn_bltzl, insn_bne, insn_break, insn_cache, insn_cfc1,
52 insn_cfcmsa, insn_ctc1, insn_ctcmsa, insn_daddiu, insn_daddu, insn_ddivu,
53 insn_di, insn_dins, insn_dinsm, insn_dinsu, insn_divu, insn_dmfc0,
54 insn_dmtc0, insn_dmultu, insn_drotr, insn_drotr32, insn_dsbh, insn_dshd,
55 insn_dsll, insn_dsll32, insn_dsllv, insn_dsra, insn_dsra32, insn_dsrav,
56 insn_dsrl, insn_dsrl32, insn_dsrlv, insn_dsubu, insn_eret, insn_ext,
57 insn_ins, insn_j, insn_jal, insn_jalr, insn_jr, insn_lb, insn_lbu,
58 insn_ld, insn_lddir, insn_ldpte, insn_ldx, insn_lh, insn_lhu,
59 insn_ll, insn_lld, insn_lui, insn_lw, insn_lwu, insn_lwx, insn_mfc0,
60 insn_mfhc0, insn_mfhi, insn_mflo, insn_movn, insn_movz, insn_mtc0,
61 insn_mthc0, insn_mthi, insn_mtlo, insn_mul, insn_multu, insn_nor,
62 insn_or, insn_ori, insn_pref, insn_rfe, insn_rotr, insn_sb,
63 insn_sc, insn_scd, insn_sd, insn_sh, insn_sll, insn_sllv,
64 insn_slt, insn_slti, insn_sltiu, insn_sltu, insn_sra, insn_srl,
James Hogan9f730a62016-06-23 17:34:37 +010065 insn_srlv, insn_subu, insn_sw, insn_sync, insn_syscall, insn_tlbp,
66 insn_tlbr, insn_tlbwi, insn_tlbwr, insn_wait, insn_wsbh, insn_xor,
David Daneydc190122017-06-13 15:28:45 -070067 insn_xori, insn_yield,
David Daneyce807d52017-06-13 15:28:43 -070068 insn_invalid /* insn_invalid must be last */
Thiemo Seufere30ec452008-01-28 20:05:38 +000069};
70
71struct insn {
Thiemo Seufere30ec452008-01-28 20:05:38 +000072 u32 match;
73 enum fields fields;
74};
75
Paul Gortmaker078a55f2013-06-18 13:38:59 +000076static inline u32 build_rs(u32 arg)
Thiemo Seufere30ec452008-01-28 20:05:38 +000077{
David Daney8d662c8d2010-12-27 18:18:29 -080078 WARN(arg & ~RS_MASK, KERN_WARNING "Micro-assembler field overflow\n");
Thiemo Seufere30ec452008-01-28 20:05:38 +000079
80 return (arg & RS_MASK) << RS_SH;
81}
82
Paul Gortmaker078a55f2013-06-18 13:38:59 +000083static inline u32 build_rt(u32 arg)
Thiemo Seufere30ec452008-01-28 20:05:38 +000084{
David Daney8d662c8d2010-12-27 18:18:29 -080085 WARN(arg & ~RT_MASK, KERN_WARNING "Micro-assembler field overflow\n");
Thiemo Seufere30ec452008-01-28 20:05:38 +000086
87 return (arg & RT_MASK) << RT_SH;
88}
89
Paul Gortmaker078a55f2013-06-18 13:38:59 +000090static inline u32 build_rd(u32 arg)
Thiemo Seufere30ec452008-01-28 20:05:38 +000091{
David Daney8d662c8d2010-12-27 18:18:29 -080092 WARN(arg & ~RD_MASK, KERN_WARNING "Micro-assembler field overflow\n");
Thiemo Seufere30ec452008-01-28 20:05:38 +000093
94 return (arg & RD_MASK) << RD_SH;
95}
96
Paul Gortmaker078a55f2013-06-18 13:38:59 +000097static inline u32 build_re(u32 arg)
Thiemo Seufere30ec452008-01-28 20:05:38 +000098{
David Daney8d662c8d2010-12-27 18:18:29 -080099 WARN(arg & ~RE_MASK, KERN_WARNING "Micro-assembler field overflow\n");
Thiemo Seufere30ec452008-01-28 20:05:38 +0000100
101 return (arg & RE_MASK) << RE_SH;
102}
103
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000104static inline u32 build_simm(s32 arg)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000105{
David Daney8d662c8d2010-12-27 18:18:29 -0800106 WARN(arg > 0x7fff || arg < -0x8000,
107 KERN_WARNING "Micro-assembler field overflow\n");
Thiemo Seufere30ec452008-01-28 20:05:38 +0000108
109 return arg & 0xffff;
110}
111
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000112static inline u32 build_uimm(u32 arg)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000113{
David Daney8d662c8d2010-12-27 18:18:29 -0800114 WARN(arg & ~IMM_MASK, KERN_WARNING "Micro-assembler field overflow\n");
Thiemo Seufere30ec452008-01-28 20:05:38 +0000115
116 return arg & IMM_MASK;
117}
118
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000119static inline u32 build_scimm(u32 arg)
David Daney58b9e222010-02-18 16:13:03 -0800120{
David Daney8d662c8d2010-12-27 18:18:29 -0800121 WARN(arg & ~SCIMM_MASK,
122 KERN_WARNING "Micro-assembler field overflow\n");
David Daney58b9e222010-02-18 16:13:03 -0800123
124 return (arg & SCIMM_MASK) << SCIMM_SH;
125}
126
Leonid Yegoshin51eec48e2014-11-18 16:24:15 +0000127static inline u32 build_scimm9(s32 arg)
128{
129 WARN((arg > 0xff || arg < -0x100),
130 KERN_WARNING "Micro-assembler field overflow\n");
131
132 return (arg & SIMM9_MASK) << SIMM9_SH;
133}
134
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000135static inline u32 build_func(u32 arg)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000136{
David Daney8d662c8d2010-12-27 18:18:29 -0800137 WARN(arg & ~FUNC_MASK, KERN_WARNING "Micro-assembler field overflow\n");
Thiemo Seufere30ec452008-01-28 20:05:38 +0000138
139 return arg & FUNC_MASK;
140}
141
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000142static inline u32 build_set(u32 arg)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000143{
David Daney8d662c8d2010-12-27 18:18:29 -0800144 WARN(arg & ~SET_MASK, KERN_WARNING "Micro-assembler field overflow\n");
Thiemo Seufere30ec452008-01-28 20:05:38 +0000145
146 return arg & SET_MASK;
147}
148
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000149static void build_insn(u32 **buf, enum opcode opc, ...);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000150
151#define I_u1u2u3(op) \
152Ip_u1u2u3(op) \
153{ \
154 build_insn(buf, insn##op, a, b, c); \
David Daney22b07632010-07-23 18:41:43 -0700155} \
156UASM_EXPORT_SYMBOL(uasm_i##op);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000157
Markos Chandras9d987362014-06-23 10:38:44 +0100158#define I_s3s1s2(op) \
159Ip_s3s1s2(op) \
160{ \
161 build_insn(buf, insn##op, b, c, a); \
162} \
163UASM_EXPORT_SYMBOL(uasm_i##op);
164
Thiemo Seufere30ec452008-01-28 20:05:38 +0000165#define I_u2u1u3(op) \
166Ip_u2u1u3(op) \
167{ \
168 build_insn(buf, insn##op, b, a, c); \
David Daney22b07632010-07-23 18:41:43 -0700169} \
170UASM_EXPORT_SYMBOL(uasm_i##op);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000171
Markos Chandrasbeef8e02014-04-08 12:47:02 +0100172#define I_u3u2u1(op) \
173Ip_u3u2u1(op) \
174{ \
175 build_insn(buf, insn##op, c, b, a); \
176} \
177UASM_EXPORT_SYMBOL(uasm_i##op);
178
Thiemo Seufere30ec452008-01-28 20:05:38 +0000179#define I_u3u1u2(op) \
180Ip_u3u1u2(op) \
181{ \
182 build_insn(buf, insn##op, b, c, a); \
David Daney22b07632010-07-23 18:41:43 -0700183} \
184UASM_EXPORT_SYMBOL(uasm_i##op);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000185
186#define I_u1u2s3(op) \
187Ip_u1u2s3(op) \
188{ \
189 build_insn(buf, insn##op, a, b, c); \
David Daney22b07632010-07-23 18:41:43 -0700190} \
191UASM_EXPORT_SYMBOL(uasm_i##op);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000192
193#define I_u2s3u1(op) \
194Ip_u2s3u1(op) \
195{ \
196 build_insn(buf, insn##op, c, a, b); \
David Daney22b07632010-07-23 18:41:43 -0700197} \
198UASM_EXPORT_SYMBOL(uasm_i##op);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000199
200#define I_u2u1s3(op) \
201Ip_u2u1s3(op) \
202{ \
203 build_insn(buf, insn##op, b, a, c); \
David Daney22b07632010-07-23 18:41:43 -0700204} \
205UASM_EXPORT_SYMBOL(uasm_i##op);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000206
David Daney92078e02009-10-14 12:16:55 -0700207#define I_u2u1msbu3(op) \
208Ip_u2u1msbu3(op) \
209{ \
210 build_insn(buf, insn##op, b, a, c+d-1, c); \
David Daney22b07632010-07-23 18:41:43 -0700211} \
212UASM_EXPORT_SYMBOL(uasm_i##op);
David Daney92078e02009-10-14 12:16:55 -0700213
David Daneyc42aef02010-12-21 14:19:10 -0800214#define I_u2u1msb32u3(op) \
215Ip_u2u1msbu3(op) \
216{ \
217 build_insn(buf, insn##op, b, a, c+d-33, c); \
218} \
219UASM_EXPORT_SYMBOL(uasm_i##op);
220
David Daneydc190122017-06-13 15:28:45 -0700221#define I_u2u1msb32msb3(op) \
222Ip_u2u1msbu3(op) \
223{ \
224 build_insn(buf, insn##op, b, a, c+d-33, c-32); \
225} \
226UASM_EXPORT_SYMBOL(uasm_i##op);
227
Ralf Baechle70342282013-01-22 12:59:30 +0100228#define I_u2u1msbdu3(op) \
Steven J. Hille6de1a02012-07-12 17:21:31 +0000229Ip_u2u1msbu3(op) \
230{ \
231 build_insn(buf, insn##op, b, a, d-1, c); \
232} \
233UASM_EXPORT_SYMBOL(uasm_i##op);
234
Thiemo Seufere30ec452008-01-28 20:05:38 +0000235#define I_u1u2(op) \
236Ip_u1u2(op) \
237{ \
238 build_insn(buf, insn##op, a, b); \
David Daney22b07632010-07-23 18:41:43 -0700239} \
240UASM_EXPORT_SYMBOL(uasm_i##op);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000241
Paul Burtond674dd12014-03-04 15:12:36 +0000242#define I_u2u1(op) \
243Ip_u1u2(op) \
244{ \
245 build_insn(buf, insn##op, b, a); \
246} \
247UASM_EXPORT_SYMBOL(uasm_i##op);
248
Thiemo Seufere30ec452008-01-28 20:05:38 +0000249#define I_u1s2(op) \
250Ip_u1s2(op) \
251{ \
252 build_insn(buf, insn##op, a, b); \
David Daney22b07632010-07-23 18:41:43 -0700253} \
254UASM_EXPORT_SYMBOL(uasm_i##op);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000255
256#define I_u1(op) \
257Ip_u1(op) \
258{ \
259 build_insn(buf, insn##op, a); \
David Daney22b07632010-07-23 18:41:43 -0700260} \
261UASM_EXPORT_SYMBOL(uasm_i##op);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000262
263#define I_0(op) \
264Ip_0(op) \
265{ \
266 build_insn(buf, insn##op); \
David Daney22b07632010-07-23 18:41:43 -0700267} \
268UASM_EXPORT_SYMBOL(uasm_i##op);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000269
270I_u2u1s3(_addiu)
271I_u3u1u2(_addu)
272I_u2u1u3(_andi)
273I_u3u1u2(_and)
274I_u1u2s3(_beq)
275I_u1u2s3(_beql)
276I_u1s2(_bgez)
277I_u1s2(_bgezl)
David Daneydc190122017-06-13 15:28:45 -0700278I_u1s2(_bgtz)
279I_u1s2(_blez)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000280I_u1s2(_bltz)
281I_u1s2(_bltzl)
282I_u1u2s3(_bne)
David Daneydc190122017-06-13 15:28:45 -0700283I_u1(_break)
Thiemo Seuferfb2a27e72008-02-18 19:32:49 +0000284I_u2s3u1(_cache)
James Hoganc29732a2016-06-23 17:34:34 +0100285I_u1u2(_cfc1)
James Hogan59e35592016-06-23 17:34:35 +0100286I_u2u1(_cfcmsa)
James Hoganc29732a2016-06-23 17:34:34 +0100287I_u1u2(_ctc1)
James Hogan59e35592016-06-23 17:34:35 +0100288I_u2u1(_ctcmsa)
David Daneydc190122017-06-13 15:28:45 -0700289I_u1u2(_ddivu)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000290I_u1u2u3(_dmfc0)
291I_u1u2u3(_dmtc0)
David Daneydc190122017-06-13 15:28:45 -0700292I_u1u2(_dmultu)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000293I_u2u1s3(_daddiu)
294I_u3u1u2(_daddu)
James Hogan61c64cf2016-06-23 17:34:36 +0100295I_u1(_di);
Markos Chandras4c12a852014-04-08 12:47:06 +0100296I_u1u2(_divu)
David Daneydc190122017-06-13 15:28:45 -0700297I_u2u1(_dsbh);
298I_u2u1(_dshd);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000299I_u2u1u3(_dsll)
300I_u2u1u3(_dsll32)
David Daneydc190122017-06-13 15:28:45 -0700301I_u3u2u1(_dsllv)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000302I_u2u1u3(_dsra)
David Daneydc190122017-06-13 15:28:45 -0700303I_u2u1u3(_dsra32)
304I_u3u2u1(_dsrav)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000305I_u2u1u3(_dsrl)
306I_u2u1u3(_dsrl32)
David Daneydc190122017-06-13 15:28:45 -0700307I_u3u2u1(_dsrlv)
David Daney92078e02009-10-14 12:16:55 -0700308I_u2u1u3(_drotr)
David Daneyde6d5b552010-07-23 18:41:41 -0700309I_u2u1u3(_drotr32)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000310I_u3u1u2(_dsubu)
311I_0(_eret)
Steven J. Hille6de1a02012-07-12 17:21:31 +0000312I_u2u1msbdu3(_ext)
313I_u2u1msbu3(_ins)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000314I_u1(_j)
315I_u1(_jal)
Paul Burton49e9529b2014-03-16 12:58:05 +0000316I_u2u1(_jalr)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000317I_u1(_jr)
Markos Chandras82488812014-04-16 13:49:57 +0100318I_u2s3u1(_lb)
David Daneydc190122017-06-13 15:28:45 -0700319I_u2s3u1(_lbu)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000320I_u2s3u1(_ld)
Markos Chandrasd6b33142014-04-08 12:47:12 +0100321I_u2s3u1(_lh)
David Daneybfbfa9d2017-03-14 14:21:40 -0700322I_u2s3u1(_lhu)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000323I_u2s3u1(_ll)
324I_u2s3u1(_lld)
325I_u1s2(_lui)
326I_u2s3u1(_lw)
David Daneydc190122017-06-13 15:28:45 -0700327I_u2s3u1(_lwu)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000328I_u1u2u3(_mfc0)
Steven J. Hille2965cd2014-11-13 09:52:02 -0600329I_u1u2u3(_mfhc0)
David Daneydc190122017-06-13 15:28:45 -0700330I_u3u1u2(_movn)
331I_u3u1u2(_movz)
Markos Chandrasf3ec7a22014-04-08 12:47:07 +0100332I_u1(_mfhi)
Markos Chandras16d21a82014-04-14 15:42:31 +0100333I_u1(_mflo)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000334I_u1u2u3(_mtc0)
Steven J. Hille2965cd2014-11-13 09:52:02 -0600335I_u1u2u3(_mthc0)
James Hogan9f730a62016-06-23 17:34:37 +0100336I_u1(_mthi)
337I_u1(_mtlo)
Markos Chandrasa8e897a2014-04-08 12:47:13 +0100338I_u3u1u2(_mul)
David Daneydc190122017-06-13 15:28:45 -0700339I_u1u2(_multu)
340I_u3u1u2(_nor)
Ralf Baechle58081842010-03-23 15:54:50 +0100341I_u3u1u2(_or)
David Daneydc190122017-06-13 15:28:45 -0700342I_u2u1u3(_ori)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000343I_0(_rfe)
David Daneydc190122017-06-13 15:28:45 -0700344I_u2s3u1(_sb)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000345I_u2s3u1(_sc)
346I_u2s3u1(_scd)
347I_u2s3u1(_sd)
David Daneydc190122017-06-13 15:28:45 -0700348I_u2s3u1(_sh)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000349I_u2u1u3(_sll)
Markos Chandrasbef581b2014-04-08 12:47:04 +0100350I_u3u2u1(_sllv)
Markos Chandras7682f9e2014-06-23 10:38:45 +0100351I_s3s1s2(_slt)
David Daneydc190122017-06-13 15:28:45 -0700352I_u2u1s3(_slti)
Markos Chandras390363e2014-04-08 12:47:09 +0100353I_u2u1s3(_sltiu)
Markos Chandrase8ef8682014-04-08 12:47:10 +0100354I_u3u1u2(_sltu)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000355I_u2u1u3(_sra)
356I_u2u1u3(_srl)
Markos Chandrasf31318f2014-04-08 12:47:05 +0100357I_u3u2u1(_srlv)
David Daney32546f32010-02-10 15:12:46 -0800358I_u2u1u3(_rotr)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000359I_u3u1u2(_subu)
360I_u2s3u1(_sw)
Paul Burton729ff562013-12-24 03:49:45 +0000361I_u1(_sync)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000362I_0(_tlbp)
David Daney32546f32010-02-10 15:12:46 -0800363I_0(_tlbr)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000364I_0(_tlbwi)
365I_0(_tlbwr)
Paul Burton53ed1382013-12-24 03:50:35 +0000366I_u1(_wait);
Markos Chandrasab9e4fa2014-04-08 12:47:11 +0100367I_u2u1(_wsbh)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000368I_u3u1u2(_xor)
369I_u2u1u3(_xori)
Paul Burtond674dd12014-03-04 15:12:36 +0000370I_u2u1(_yield)
David Daney92078e02009-10-14 12:16:55 -0700371I_u2u1msbu3(_dins);
David Daneyc42aef02010-12-21 14:19:10 -0800372I_u2u1msb32u3(_dinsm);
David Daneydc190122017-06-13 15:28:45 -0700373I_u2u1msb32msb3(_dinsu);
David Daney58b9e222010-02-18 16:13:03 -0800374I_u1(_syscall);
David Daney5b97c3f2010-07-23 18:41:42 -0700375I_u1u2s3(_bbit0);
376I_u1u2s3(_bbit1);
David Daneybb3d68c2010-12-27 18:07:56 -0800377I_u3u1u2(_lwx)
378I_u3u1u2(_ldx)
Huacai Chen380cd582016-03-03 09:45:12 +0800379I_u1u2(_ldpte)
380I_u2u1u3(_lddir)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000381
David Daneyc9941152010-10-07 16:03:53 -0700382#ifdef CONFIG_CPU_CAVIUM_OCTEON
383#include <asm/octeon/octeon.h>
Paul Burton33679a52017-03-30 14:52:15 -0700384void uasm_i_pref(u32 **buf, unsigned int a, signed int b,
David Daneyc9941152010-10-07 16:03:53 -0700385 unsigned int c)
386{
David Daneye3d0ead2015-01-15 16:11:13 +0300387 if (CAVIUM_OCTEON_DCACHE_PREFETCH_WAR && a <= 24 && a != 5)
David Daneyc9941152010-10-07 16:03:53 -0700388 /*
389 * As per erratum Core-14449, replace prefetches 0-4,
390 * 6-24 with 'pref 28'.
391 */
392 build_insn(buf, insn_pref, c, 28, b);
393 else
394 build_insn(buf, insn_pref, c, a, b);
395}
Paul Burton33679a52017-03-30 14:52:15 -0700396UASM_EXPORT_SYMBOL(uasm_i_pref);
David Daneyc9941152010-10-07 16:03:53 -0700397#else
398I_u2s3u1(_pref)
399#endif
400
Thiemo Seufere30ec452008-01-28 20:05:38 +0000401/* Handle labels. */
Paul Burton33679a52017-03-30 14:52:15 -0700402void uasm_build_label(struct uasm_label **lab, u32 *addr, int lid)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000403{
404 (*lab)->addr = addr;
405 (*lab)->lab = lid;
406 (*lab)++;
407}
Paul Burton33679a52017-03-30 14:52:15 -0700408UASM_EXPORT_SYMBOL(uasm_build_label);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000409
Paul Burton33679a52017-03-30 14:52:15 -0700410int uasm_in_compat_space_p(long addr)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000411{
412 /* Is this address in 32bit compat space? */
James Hoganf7d9afe2016-07-08 14:05:26 +0100413 return addr == (int)addr;
Thiemo Seufere30ec452008-01-28 20:05:38 +0000414}
Paul Burton33679a52017-03-30 14:52:15 -0700415UASM_EXPORT_SYMBOL(uasm_in_compat_space_p);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000416
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000417static int uasm_rel_highest(long val)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000418{
419#ifdef CONFIG_64BIT
420 return ((((val + 0x800080008000L) >> 48) & 0xffff) ^ 0x8000) - 0x8000;
421#else
422 return 0;
423#endif
424}
425
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000426static int uasm_rel_higher(long val)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000427{
428#ifdef CONFIG_64BIT
429 return ((((val + 0x80008000L) >> 32) & 0xffff) ^ 0x8000) - 0x8000;
430#else
431 return 0;
432#endif
433}
434
Paul Burton33679a52017-03-30 14:52:15 -0700435int uasm_rel_hi(long val)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000436{
437 return ((((val + 0x8000L) >> 16) & 0xffff) ^ 0x8000) - 0x8000;
438}
Paul Burton33679a52017-03-30 14:52:15 -0700439UASM_EXPORT_SYMBOL(uasm_rel_hi);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000440
Paul Burton33679a52017-03-30 14:52:15 -0700441int uasm_rel_lo(long val)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000442{
443 return ((val & 0xffff) ^ 0x8000) - 0x8000;
444}
Paul Burton33679a52017-03-30 14:52:15 -0700445UASM_EXPORT_SYMBOL(uasm_rel_lo);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000446
Paul Burton33679a52017-03-30 14:52:15 -0700447void UASM_i_LA_mostly(u32 **buf, unsigned int rs, long addr)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000448{
Paul Burton33679a52017-03-30 14:52:15 -0700449 if (!uasm_in_compat_space_p(addr)) {
450 uasm_i_lui(buf, rs, uasm_rel_highest(addr));
Thiemo Seufere30ec452008-01-28 20:05:38 +0000451 if (uasm_rel_higher(addr))
Paul Burton33679a52017-03-30 14:52:15 -0700452 uasm_i_daddiu(buf, rs, rs, uasm_rel_higher(addr));
453 if (uasm_rel_hi(addr)) {
454 uasm_i_dsll(buf, rs, rs, 16);
455 uasm_i_daddiu(buf, rs, rs,
456 uasm_rel_hi(addr));
457 uasm_i_dsll(buf, rs, rs, 16);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000458 } else
Paul Burton33679a52017-03-30 14:52:15 -0700459 uasm_i_dsll32(buf, rs, rs, 0);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000460 } else
Paul Burton33679a52017-03-30 14:52:15 -0700461 uasm_i_lui(buf, rs, uasm_rel_hi(addr));
Thiemo Seufere30ec452008-01-28 20:05:38 +0000462}
Paul Burton33679a52017-03-30 14:52:15 -0700463UASM_EXPORT_SYMBOL(UASM_i_LA_mostly);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000464
Paul Burton33679a52017-03-30 14:52:15 -0700465void UASM_i_LA(u32 **buf, unsigned int rs, long addr)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000466{
Paul Burton33679a52017-03-30 14:52:15 -0700467 UASM_i_LA_mostly(buf, rs, addr);
468 if (uasm_rel_lo(addr)) {
469 if (!uasm_in_compat_space_p(addr))
470 uasm_i_daddiu(buf, rs, rs,
471 uasm_rel_lo(addr));
Thiemo Seufere30ec452008-01-28 20:05:38 +0000472 else
Paul Burton33679a52017-03-30 14:52:15 -0700473 uasm_i_addiu(buf, rs, rs,
474 uasm_rel_lo(addr));
Thiemo Seufere30ec452008-01-28 20:05:38 +0000475 }
476}
Paul Burton33679a52017-03-30 14:52:15 -0700477UASM_EXPORT_SYMBOL(UASM_i_LA);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000478
479/* Handle relocations. */
Paul Burton33679a52017-03-30 14:52:15 -0700480void uasm_r_mips_pc16(struct uasm_reloc **rel, u32 *addr, int lid)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000481{
482 (*rel)->addr = addr;
483 (*rel)->type = R_MIPS_PC16;
484 (*rel)->lab = lid;
485 (*rel)++;
486}
Paul Burton33679a52017-03-30 14:52:15 -0700487UASM_EXPORT_SYMBOL(uasm_r_mips_pc16);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000488
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000489static inline void __resolve_relocs(struct uasm_reloc *rel,
490 struct uasm_label *lab);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000491
Paul Burton33679a52017-03-30 14:52:15 -0700492void uasm_resolve_relocs(struct uasm_reloc *rel,
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000493 struct uasm_label *lab)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000494{
495 struct uasm_label *l;
496
497 for (; rel->lab != UASM_LABEL_INVALID; rel++)
498 for (l = lab; l->lab != UASM_LABEL_INVALID; l++)
499 if (rel->lab == l->lab)
500 __resolve_relocs(rel, l);
501}
Paul Burton33679a52017-03-30 14:52:15 -0700502UASM_EXPORT_SYMBOL(uasm_resolve_relocs);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000503
Paul Burton33679a52017-03-30 14:52:15 -0700504void uasm_move_relocs(struct uasm_reloc *rel, u32 *first, u32 *end,
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000505 long off)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000506{
507 for (; rel->lab != UASM_LABEL_INVALID; rel++)
508 if (rel->addr >= first && rel->addr < end)
509 rel->addr += off;
510}
Paul Burton33679a52017-03-30 14:52:15 -0700511UASM_EXPORT_SYMBOL(uasm_move_relocs);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000512
Paul Burton33679a52017-03-30 14:52:15 -0700513void uasm_move_labels(struct uasm_label *lab, u32 *first, u32 *end,
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000514 long off)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000515{
516 for (; lab->lab != UASM_LABEL_INVALID; lab++)
517 if (lab->addr >= first && lab->addr < end)
518 lab->addr += off;
519}
Paul Burton33679a52017-03-30 14:52:15 -0700520UASM_EXPORT_SYMBOL(uasm_move_labels);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000521
Paul Burton33679a52017-03-30 14:52:15 -0700522void uasm_copy_handler(struct uasm_reloc *rel, struct uasm_label *lab,
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000523 u32 *first, u32 *end, u32 *target)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000524{
525 long off = (long)(target - first);
526
527 memcpy(target, first, (end - first) * sizeof(u32));
528
Paul Burton33679a52017-03-30 14:52:15 -0700529 uasm_move_relocs(rel, first, end, off);
530 uasm_move_labels(lab, first, end, off);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000531}
Paul Burton33679a52017-03-30 14:52:15 -0700532UASM_EXPORT_SYMBOL(uasm_copy_handler);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000533
Paul Burton33679a52017-03-30 14:52:15 -0700534int uasm_insn_has_bdelay(struct uasm_reloc *rel, u32 *addr)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000535{
536 for (; rel->lab != UASM_LABEL_INVALID; rel++) {
537 if (rel->addr == addr
538 && (rel->type == R_MIPS_PC16
539 || rel->type == R_MIPS_26))
540 return 1;
541 }
542
543 return 0;
544}
Paul Burton33679a52017-03-30 14:52:15 -0700545UASM_EXPORT_SYMBOL(uasm_insn_has_bdelay);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000546
547/* Convenience functions for labeled branches. */
Paul Burton33679a52017-03-30 14:52:15 -0700548void uasm_il_bltz(u32 **p, struct uasm_reloc **r, unsigned int reg,
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000549 int lid)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000550{
551 uasm_r_mips_pc16(r, *p, lid);
Paul Burton33679a52017-03-30 14:52:15 -0700552 uasm_i_bltz(p, reg, 0);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000553}
Paul Burton33679a52017-03-30 14:52:15 -0700554UASM_EXPORT_SYMBOL(uasm_il_bltz);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000555
Paul Burton33679a52017-03-30 14:52:15 -0700556void uasm_il_b(u32 **p, struct uasm_reloc **r, int lid)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000557{
558 uasm_r_mips_pc16(r, *p, lid);
Paul Burton33679a52017-03-30 14:52:15 -0700559 uasm_i_b(p, 0);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000560}
Paul Burton33679a52017-03-30 14:52:15 -0700561UASM_EXPORT_SYMBOL(uasm_il_b);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000562
Paul Burton33679a52017-03-30 14:52:15 -0700563void uasm_il_beq(u32 **p, struct uasm_reloc **r, unsigned int r1,
Paul Burton8dee5902013-12-24 03:51:39 +0000564 unsigned int r2, int lid)
565{
566 uasm_r_mips_pc16(r, *p, lid);
Paul Burton33679a52017-03-30 14:52:15 -0700567 uasm_i_beq(p, r1, r2, 0);
Paul Burton8dee5902013-12-24 03:51:39 +0000568}
Paul Burton33679a52017-03-30 14:52:15 -0700569UASM_EXPORT_SYMBOL(uasm_il_beq);
Paul Burton8dee5902013-12-24 03:51:39 +0000570
Paul Burton33679a52017-03-30 14:52:15 -0700571void uasm_il_beqz(u32 **p, struct uasm_reloc **r, unsigned int reg,
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000572 int lid)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000573{
574 uasm_r_mips_pc16(r, *p, lid);
Paul Burton33679a52017-03-30 14:52:15 -0700575 uasm_i_beqz(p, reg, 0);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000576}
Paul Burton33679a52017-03-30 14:52:15 -0700577UASM_EXPORT_SYMBOL(uasm_il_beqz);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000578
Paul Burton33679a52017-03-30 14:52:15 -0700579void uasm_il_beqzl(u32 **p, struct uasm_reloc **r, unsigned int reg,
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000580 int lid)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000581{
582 uasm_r_mips_pc16(r, *p, lid);
Paul Burton33679a52017-03-30 14:52:15 -0700583 uasm_i_beqzl(p, reg, 0);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000584}
Paul Burton33679a52017-03-30 14:52:15 -0700585UASM_EXPORT_SYMBOL(uasm_il_beqzl);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000586
Paul Burton33679a52017-03-30 14:52:15 -0700587void uasm_il_bne(u32 **p, struct uasm_reloc **r, unsigned int reg1,
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000588 unsigned int reg2, int lid)
Thiemo Seuferfb2a27e72008-02-18 19:32:49 +0000589{
590 uasm_r_mips_pc16(r, *p, lid);
Paul Burton33679a52017-03-30 14:52:15 -0700591 uasm_i_bne(p, reg1, reg2, 0);
Thiemo Seuferfb2a27e72008-02-18 19:32:49 +0000592}
Paul Burton33679a52017-03-30 14:52:15 -0700593UASM_EXPORT_SYMBOL(uasm_il_bne);
Thiemo Seuferfb2a27e72008-02-18 19:32:49 +0000594
Paul Burton33679a52017-03-30 14:52:15 -0700595void uasm_il_bnez(u32 **p, struct uasm_reloc **r, unsigned int reg,
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000596 int lid)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000597{
598 uasm_r_mips_pc16(r, *p, lid);
Paul Burton33679a52017-03-30 14:52:15 -0700599 uasm_i_bnez(p, reg, 0);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000600}
Paul Burton33679a52017-03-30 14:52:15 -0700601UASM_EXPORT_SYMBOL(uasm_il_bnez);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000602
Paul Burton33679a52017-03-30 14:52:15 -0700603void uasm_il_bgezl(u32 **p, struct uasm_reloc **r, unsigned int reg,
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000604 int lid)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000605{
606 uasm_r_mips_pc16(r, *p, lid);
Paul Burton33679a52017-03-30 14:52:15 -0700607 uasm_i_bgezl(p, reg, 0);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000608}
Paul Burton33679a52017-03-30 14:52:15 -0700609UASM_EXPORT_SYMBOL(uasm_il_bgezl);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000610
Paul Burton33679a52017-03-30 14:52:15 -0700611void uasm_il_bgez(u32 **p, struct uasm_reloc **r, unsigned int reg,
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000612 int lid)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000613{
614 uasm_r_mips_pc16(r, *p, lid);
Paul Burton33679a52017-03-30 14:52:15 -0700615 uasm_i_bgez(p, reg, 0);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000616}
Paul Burton33679a52017-03-30 14:52:15 -0700617UASM_EXPORT_SYMBOL(uasm_il_bgez);
David Daney5b97c3f2010-07-23 18:41:42 -0700618
Paul Burton33679a52017-03-30 14:52:15 -0700619void uasm_il_bbit0(u32 **p, struct uasm_reloc **r, unsigned int reg,
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000620 unsigned int bit, int lid)
David Daney5b97c3f2010-07-23 18:41:42 -0700621{
622 uasm_r_mips_pc16(r, *p, lid);
Paul Burton33679a52017-03-30 14:52:15 -0700623 uasm_i_bbit0(p, reg, bit, 0);
David Daney5b97c3f2010-07-23 18:41:42 -0700624}
Paul Burton33679a52017-03-30 14:52:15 -0700625UASM_EXPORT_SYMBOL(uasm_il_bbit0);
David Daney5b97c3f2010-07-23 18:41:42 -0700626
Paul Burton33679a52017-03-30 14:52:15 -0700627void uasm_il_bbit1(u32 **p, struct uasm_reloc **r, unsigned int reg,
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000628 unsigned int bit, int lid)
David Daney5b97c3f2010-07-23 18:41:42 -0700629{
630 uasm_r_mips_pc16(r, *p, lid);
Paul Burton33679a52017-03-30 14:52:15 -0700631 uasm_i_bbit1(p, reg, bit, 0);
David Daney5b97c3f2010-07-23 18:41:42 -0700632}
Paul Burton33679a52017-03-30 14:52:15 -0700633UASM_EXPORT_SYMBOL(uasm_il_bbit1);