altera.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528
  1. /*
  2. * altera.c
  3. *
  4. * altera FPGA driver
  5. *
  6. * Copyright (C) Altera Corporation 1998-2001
  7. * Copyright (C) 2010,2011 NetUP Inc.
  8. * Copyright (C) 2010,2011 Igor M. Liplianin <liplianin@netup.ru>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. *
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. */
  25. #include <asm/unaligned.h>
  26. #include <linux/ctype.h>
  27. #include <linux/string.h>
  28. #include <linux/firmware.h>
  29. #include <linux/slab.h>
  30. #include "altera.h"
  31. #include "altera-exprt.h"
  32. #include "altera-jtag.h"
  33. static int debug = 1;
  34. module_param(debug, int, 0644);
  35. MODULE_PARM_DESC(debug, "enable debugging information");
  36. MODULE_DESCRIPTION("altera FPGA kernel module");
  37. MODULE_AUTHOR("Igor M. Liplianin <liplianin@netup.ru>");
  38. MODULE_LICENSE("GPL");
  39. #define dprintk(args...) \
  40. if (debug) { \
  41. printk(KERN_DEBUG args); \
  42. }
  43. enum altera_fpga_opcode {
  44. OP_NOP = 0,
  45. OP_DUP,
  46. OP_SWP,
  47. OP_ADD,
  48. OP_SUB,
  49. OP_MULT,
  50. OP_DIV,
  51. OP_MOD,
  52. OP_SHL,
  53. OP_SHR,
  54. OP_NOT,
  55. OP_AND,
  56. OP_OR,
  57. OP_XOR,
  58. OP_INV,
  59. OP_GT,
  60. OP_LT,
  61. OP_RET,
  62. OP_CMPS,
  63. OP_PINT,
  64. OP_PRNT,
  65. OP_DSS,
  66. OP_DSSC,
  67. OP_ISS,
  68. OP_ISSC,
  69. OP_DPR = 0x1c,
  70. OP_DPRL,
  71. OP_DPO,
  72. OP_DPOL,
  73. OP_IPR,
  74. OP_IPRL,
  75. OP_IPO,
  76. OP_IPOL,
  77. OP_PCHR,
  78. OP_EXIT,
  79. OP_EQU,
  80. OP_POPT,
  81. OP_ABS = 0x2c,
  82. OP_BCH0,
  83. OP_PSH0 = 0x2f,
  84. OP_PSHL = 0x40,
  85. OP_PSHV,
  86. OP_JMP,
  87. OP_CALL,
  88. OP_NEXT,
  89. OP_PSTR,
  90. OP_SINT = 0x47,
  91. OP_ST,
  92. OP_ISTP,
  93. OP_DSTP,
  94. OP_SWPN,
  95. OP_DUPN,
  96. OP_POPV,
  97. OP_POPE,
  98. OP_POPA,
  99. OP_JMPZ,
  100. OP_DS,
  101. OP_IS,
  102. OP_DPRA,
  103. OP_DPOA,
  104. OP_IPRA,
  105. OP_IPOA,
  106. OP_EXPT,
  107. OP_PSHE,
  108. OP_PSHA,
  109. OP_DYNA,
  110. OP_EXPV = 0x5c,
  111. OP_COPY = 0x80,
  112. OP_REVA,
  113. OP_DSC,
  114. OP_ISC,
  115. OP_WAIT,
  116. OP_VS,
  117. OP_CMPA = 0xc0,
  118. OP_VSC,
  119. };
  120. struct altera_procinfo {
  121. char *name;
  122. u8 attrs;
  123. struct altera_procinfo *next;
  124. };
  125. /* This function checks if enough parameters are available on the stack. */
  126. static int altera_check_stack(int stack_ptr, int count, int *status)
  127. {
  128. if (stack_ptr < count) {
  129. *status = -EOVERFLOW;
  130. return 0;
  131. }
  132. return 1;
  133. }
  134. static void altera_export_int(char *key, s32 value)
  135. {
  136. dprintk("Export: key = \"%s\", value = %d\n", key, value);
  137. }
  138. #define HEX_LINE_CHARS 72
  139. #define HEX_LINE_BITS (HEX_LINE_CHARS * 4)
  140. static void altera_export_bool_array(char *key, u8 *data, s32 count)
  141. {
  142. char string[HEX_LINE_CHARS + 1];
  143. s32 i, offset;
  144. u32 size, line, lines, linebits, value, j, k;
  145. if (count > HEX_LINE_BITS) {
  146. dprintk("Export: key = \"%s\", %d bits, value = HEX\n",
  147. key, count);
  148. lines = (count + (HEX_LINE_BITS - 1)) / HEX_LINE_BITS;
  149. for (line = 0; line < lines; ++line) {
  150. if (line < (lines - 1)) {
  151. linebits = HEX_LINE_BITS;
  152. size = HEX_LINE_CHARS;
  153. offset = count - ((line + 1) * HEX_LINE_BITS);
  154. } else {
  155. linebits =
  156. count - ((lines - 1) * HEX_LINE_BITS);
  157. size = (linebits + 3) / 4;
  158. offset = 0L;
  159. }
  160. string[size] = '\0';
  161. j = size - 1;
  162. value = 0;
  163. for (k = 0; k < linebits; ++k) {
  164. i = k + offset;
  165. if (data[i >> 3] & (1 << (i & 7)))
  166. value |= (1 << (i & 3));
  167. if ((i & 3) == 3) {
  168. sprintf(&string[j], "%1x", value);
  169. value = 0;
  170. --j;
  171. }
  172. }
  173. if ((k & 3) > 0)
  174. sprintf(&string[j], "%1x", value);
  175. dprintk("%s\n", string);
  176. }
  177. } else {
  178. size = (count + 3) / 4;
  179. string[size] = '\0';
  180. j = size - 1;
  181. value = 0;
  182. for (i = 0; i < count; ++i) {
  183. if (data[i >> 3] & (1 << (i & 7)))
  184. value |= (1 << (i & 3));
  185. if ((i & 3) == 3) {
  186. sprintf(&string[j], "%1x", value);
  187. value = 0;
  188. --j;
  189. }
  190. }
  191. if ((i & 3) > 0)
  192. sprintf(&string[j], "%1x", value);
  193. dprintk("Export: key = \"%s\", %d bits, value = HEX %s\n",
  194. key, count, string);
  195. }
  196. }
  197. static int altera_execute(struct altera_state *astate,
  198. u8 *p,
  199. s32 program_size,
  200. s32 *error_address,
  201. int *exit_code,
  202. int *format_version)
  203. {
  204. struct altera_config *aconf = astate->config;
  205. char *msg_buff = astate->msg_buff;
  206. long *stack = astate->stack;
  207. int status = 0;
  208. u32 first_word = 0L;
  209. u32 action_table = 0L;
  210. u32 proc_table = 0L;
  211. u32 str_table = 0L;
  212. u32 sym_table = 0L;
  213. u32 data_sect = 0L;
  214. u32 code_sect = 0L;
  215. u32 debug_sect = 0L;
  216. u32 action_count = 0L;
  217. u32 proc_count = 0L;
  218. u32 sym_count = 0L;
  219. long *vars = NULL;
  220. s32 *var_size = NULL;
  221. char *attrs = NULL;
  222. u8 *proc_attributes = NULL;
  223. u32 pc;
  224. u32 opcode_address;
  225. u32 args[3];
  226. u32 opcode;
  227. u32 name_id;
  228. u8 charbuf[4];
  229. long long_tmp;
  230. u32 variable_id;
  231. u8 *charptr_tmp;
  232. u8 *charptr_tmp2;
  233. long *longptr_tmp;
  234. int version = 0;
  235. int delta = 0;
  236. int stack_ptr = 0;
  237. u32 arg_count;
  238. int done = 0;
  239. int bad_opcode = 0;
  240. u32 count;
  241. u32 index;
  242. u32 index2;
  243. s32 long_count;
  244. s32 long_idx;
  245. s32 long_idx2;
  246. u32 i;
  247. u32 j;
  248. u32 uncomp_size;
  249. u32 offset;
  250. u32 value;
  251. int current_proc = 0;
  252. int reverse;
  253. char *name;
  254. dprintk("%s\n", __func__);
  255. /* Read header information */
  256. if (program_size > 52L) {
  257. first_word = get_unaligned_be32(&p[0]);
  258. version = (first_word & 1L);
  259. *format_version = version + 1;
  260. delta = version * 8;
  261. action_table = get_unaligned_be32(&p[4]);
  262. proc_table = get_unaligned_be32(&p[8]);
  263. str_table = get_unaligned_be32(&p[4 + delta]);
  264. sym_table = get_unaligned_be32(&p[16 + delta]);
  265. data_sect = get_unaligned_be32(&p[20 + delta]);
  266. code_sect = get_unaligned_be32(&p[24 + delta]);
  267. debug_sect = get_unaligned_be32(&p[28 + delta]);
  268. action_count = get_unaligned_be32(&p[40 + delta]);
  269. proc_count = get_unaligned_be32(&p[44 + delta]);
  270. sym_count = get_unaligned_be32(&p[48 + (2 * delta)]);
  271. }
  272. if ((first_word != 0x4A414D00L) && (first_word != 0x4A414D01L)) {
  273. done = 1;
  274. status = -EIO;
  275. goto exit_done;
  276. }
  277. if (sym_count <= 0)
  278. goto exit_done;
  279. vars = kzalloc(sym_count * sizeof(long), GFP_KERNEL);
  280. if (vars == NULL)
  281. status = -ENOMEM;
  282. if (status == 0) {
  283. var_size = kzalloc(sym_count * sizeof(s32), GFP_KERNEL);
  284. if (var_size == NULL)
  285. status = -ENOMEM;
  286. }
  287. if (status == 0) {
  288. attrs = kzalloc(sym_count, GFP_KERNEL);
  289. if (attrs == NULL)
  290. status = -ENOMEM;
  291. }
  292. if ((status == 0) && (version > 0)) {
  293. proc_attributes = kzalloc(proc_count, GFP_KERNEL);
  294. if (proc_attributes == NULL)
  295. status = -ENOMEM;
  296. }
  297. if (status != 0)
  298. goto exit_done;
  299. delta = version * 2;
  300. for (i = 0; i < sym_count; ++i) {
  301. offset = (sym_table + ((11 + delta) * i));
  302. value = get_unaligned_be32(&p[offset + 3 + delta]);
  303. attrs[i] = p[offset];
  304. /*
  305. * use bit 7 of attribute byte to indicate that
  306. * this buffer was dynamically allocated
  307. * and should be freed later
  308. */
  309. attrs[i] &= 0x7f;
  310. var_size[i] = get_unaligned_be32(&p[offset + 7 + delta]);
  311. /*
  312. * Attribute bits:
  313. * bit 0: 0 = read-only, 1 = read-write
  314. * bit 1: 0 = not compressed, 1 = compressed
  315. * bit 2: 0 = not initialized, 1 = initialized
  316. * bit 3: 0 = scalar, 1 = array
  317. * bit 4: 0 = Boolean, 1 = integer
  318. * bit 5: 0 = declared variable,
  319. * 1 = compiler created temporary variable
  320. */
  321. if ((attrs[i] & 0x0c) == 0x04)
  322. /* initialized scalar variable */
  323. vars[i] = value;
  324. else if ((attrs[i] & 0x1e) == 0x0e) {
  325. /* initialized compressed Boolean array */
  326. uncomp_size = get_unaligned_le32(&p[data_sect + value]);
  327. /* allocate a buffer for the uncompressed data */
  328. vars[i] = (long)kzalloc(uncomp_size, GFP_KERNEL);
  329. if (vars[i] == 0L)
  330. status = -ENOMEM;
  331. else {
  332. /* set flag so buffer will be freed later */
  333. attrs[i] |= 0x80;
  334. /* uncompress the data */
  335. if (altera_shrink(&p[data_sect + value],
  336. var_size[i],
  337. (u8 *)vars[i],
  338. uncomp_size,
  339. version) != uncomp_size)
  340. /* decompression failed */
  341. status = -EIO;
  342. else
  343. var_size[i] = uncomp_size * 8L;
  344. }
  345. } else if ((attrs[i] & 0x1e) == 0x0c) {
  346. /* initialized Boolean array */
  347. vars[i] = value + data_sect + (long)p;
  348. } else if ((attrs[i] & 0x1c) == 0x1c) {
  349. /* initialized integer array */
  350. vars[i] = value + data_sect;
  351. } else if ((attrs[i] & 0x0c) == 0x08) {
  352. /* uninitialized array */
  353. /* flag attrs so that memory is freed */
  354. attrs[i] |= 0x80;
  355. if (var_size[i] > 0) {
  356. u32 size;
  357. if (attrs[i] & 0x10)
  358. /* integer array */
  359. size = (var_size[i] * sizeof(s32));
  360. else
  361. /* Boolean array */
  362. size = ((var_size[i] + 7L) / 8L);
  363. vars[i] = (long)kzalloc(size, GFP_KERNEL);
  364. if (vars[i] == 0) {
  365. status = -ENOMEM;
  366. } else {
  367. /* zero out memory */
  368. for (j = 0; j < size; ++j)
  369. ((u8 *)(vars[i]))[j] = 0;
  370. }
  371. } else
  372. vars[i] = 0;
  373. } else
  374. vars[i] = 0;
  375. }
  376. exit_done:
  377. if (status != 0)
  378. done = 1;
  379. altera_jinit(astate);
  380. pc = code_sect;
  381. msg_buff[0] = '\0';
  382. /*
  383. * For JBC version 2, we will execute the procedures corresponding to
  384. * the selected ACTION
  385. */
  386. if (version > 0) {
  387. if (aconf->action == NULL) {
  388. status = -EINVAL;
  389. done = 1;
  390. } else {
  391. int action_found = 0;
  392. for (i = 0; (i < action_count) && !action_found; ++i) {
  393. name_id = get_unaligned_be32(&p[action_table +
  394. (12 * i)]);
  395. name = &p[str_table + name_id];
  396. if (strnicmp(aconf->action, name, strlen(name)) == 0) {
  397. action_found = 1;
  398. current_proc =
  399. get_unaligned_be32(&p[action_table +
  400. (12 * i) + 8]);
  401. }
  402. }
  403. if (!action_found) {
  404. status = -EINVAL;
  405. done = 1;
  406. }
  407. }
  408. if (status == 0) {
  409. int first_time = 1;
  410. i = current_proc;
  411. while ((i != 0) || first_time) {
  412. first_time = 0;
  413. /* check procedure attribute byte */
  414. proc_attributes[i] =
  415. (p[proc_table +
  416. (13 * i) + 8] &
  417. 0x03);
  418. /*
  419. * BIT0 - OPTIONAL
  420. * BIT1 - RECOMMENDED
  421. * BIT6 - FORCED OFF
  422. * BIT7 - FORCED ON
  423. */
  424. i = get_unaligned_be32(&p[proc_table +
  425. (13 * i) + 4]);
  426. }
  427. /*
  428. * Set current_proc to the first procedure
  429. * to be executed
  430. */
  431. i = current_proc;
  432. while ((i != 0) &&
  433. ((proc_attributes[i] == 1) ||
  434. ((proc_attributes[i] & 0xc0) == 0x40))) {
  435. i = get_unaligned_be32(&p[proc_table +
  436. (13 * i) + 4]);
  437. }
  438. if ((i != 0) || ((i == 0) && (current_proc == 0) &&
  439. ((proc_attributes[0] != 1) &&
  440. ((proc_attributes[0] & 0xc0) != 0x40)))) {
  441. current_proc = i;
  442. pc = code_sect +
  443. get_unaligned_be32(&p[proc_table +
  444. (13 * i) + 9]);
  445. if ((pc < code_sect) || (pc >= debug_sect))
  446. status = -ERANGE;
  447. } else
  448. /* there are no procedures to execute! */
  449. done = 1;
  450. }
  451. }
  452. msg_buff[0] = '\0';
  453. while (!done) {
  454. opcode = (p[pc] & 0xff);
  455. opcode_address = pc;
  456. ++pc;
  457. if (debug > 1)
  458. printk("opcode: %02x\n", opcode);
  459. arg_count = (opcode >> 6) & 3;
  460. for (i = 0; i < arg_count; ++i) {
  461. args[i] = get_unaligned_be32(&p[pc]);
  462. pc += 4;
  463. }
  464. switch (opcode) {
  465. case OP_NOP:
  466. break;
  467. case OP_DUP:
  468. if (altera_check_stack(stack_ptr, 1, &status)) {
  469. stack[stack_ptr] = stack[stack_ptr - 1];
  470. ++stack_ptr;
  471. }
  472. break;
  473. case OP_SWP:
  474. if (altera_check_stack(stack_ptr, 2, &status)) {
  475. long_tmp = stack[stack_ptr - 2];
  476. stack[stack_ptr - 2] = stack[stack_ptr - 1];
  477. stack[stack_ptr - 1] = long_tmp;
  478. }
  479. break;
  480. case OP_ADD:
  481. if (altera_check_stack(stack_ptr, 2, &status)) {
  482. --stack_ptr;
  483. stack[stack_ptr - 1] += stack[stack_ptr];
  484. }
  485. break;
  486. case OP_SUB:
  487. if (altera_check_stack(stack_ptr, 2, &status)) {
  488. --stack_ptr;
  489. stack[stack_ptr - 1] -= stack[stack_ptr];
  490. }
  491. break;
  492. case OP_MULT:
  493. if (altera_check_stack(stack_ptr, 2, &status)) {
  494. --stack_ptr;
  495. stack[stack_ptr - 1] *= stack[stack_ptr];
  496. }
  497. break;
  498. case OP_DIV:
  499. if (altera_check_stack(stack_ptr, 2, &status)) {
  500. --stack_ptr;
  501. stack[stack_ptr - 1] /= stack[stack_ptr];
  502. }
  503. break;
  504. case OP_MOD:
  505. if (altera_check_stack(stack_ptr, 2, &status)) {
  506. --stack_ptr;
  507. stack[stack_ptr - 1] %= stack[stack_ptr];
  508. }
  509. break;
  510. case OP_SHL:
  511. if (altera_check_stack(stack_ptr, 2, &status)) {
  512. --stack_ptr;
  513. stack[stack_ptr - 1] <<= stack[stack_ptr];
  514. }
  515. break;
  516. case OP_SHR:
  517. if (altera_check_stack(stack_ptr, 2, &status)) {
  518. --stack_ptr;
  519. stack[stack_ptr - 1] >>= stack[stack_ptr];
  520. }
  521. break;
  522. case OP_NOT:
  523. if (altera_check_stack(stack_ptr, 1, &status))
  524. stack[stack_ptr - 1] ^= (-1L);
  525. break;
  526. case OP_AND:
  527. if (altera_check_stack(stack_ptr, 2, &status)) {
  528. --stack_ptr;
  529. stack[stack_ptr - 1] &= stack[stack_ptr];
  530. }
  531. break;
  532. case OP_OR:
  533. if (altera_check_stack(stack_ptr, 2, &status)) {
  534. --stack_ptr;
  535. stack[stack_ptr - 1] |= stack[stack_ptr];
  536. }
  537. break;
  538. case OP_XOR:
  539. if (altera_check_stack(stack_ptr, 2, &status)) {
  540. --stack_ptr;
  541. stack[stack_ptr - 1] ^= stack[stack_ptr];
  542. }
  543. break;
  544. case OP_INV:
  545. if (!altera_check_stack(stack_ptr, 1, &status))
  546. break;
  547. stack[stack_ptr - 1] = stack[stack_ptr - 1] ? 0L : 1L;
  548. break;
  549. case OP_GT:
  550. if (!altera_check_stack(stack_ptr, 2, &status))
  551. break;
  552. --stack_ptr;
  553. stack[stack_ptr - 1] =
  554. (stack[stack_ptr - 1] > stack[stack_ptr]) ?
  555. 1L : 0L;
  556. break;
  557. case OP_LT:
  558. if (!altera_check_stack(stack_ptr, 2, &status))
  559. break;
  560. --stack_ptr;
  561. stack[stack_ptr - 1] =
  562. (stack[stack_ptr - 1] < stack[stack_ptr]) ?
  563. 1L : 0L;
  564. break;
  565. case OP_RET:
  566. if ((version > 0) && (stack_ptr == 0)) {
  567. /*
  568. * We completed one of the main procedures
  569. * of an ACTION.
  570. * Find the next procedure
  571. * to be executed and jump to it.
  572. * If there are no more procedures, then EXIT.
  573. */
  574. i = get_unaligned_be32(&p[proc_table +
  575. (13 * current_proc) + 4]);
  576. while ((i != 0) &&
  577. ((proc_attributes[i] == 1) ||
  578. ((proc_attributes[i] & 0xc0) == 0x40)))
  579. i = get_unaligned_be32(&p[proc_table +
  580. (13 * i) + 4]);
  581. if (i == 0) {
  582. /* no procedures to execute! */
  583. done = 1;
  584. *exit_code = 0; /* success */
  585. } else {
  586. current_proc = i;
  587. pc = code_sect + get_unaligned_be32(
  588. &p[proc_table +
  589. (13 * i) + 9]);
  590. if ((pc < code_sect) ||
  591. (pc >= debug_sect))
  592. status = -ERANGE;
  593. }
  594. } else
  595. if (altera_check_stack(stack_ptr, 1, &status)) {
  596. pc = stack[--stack_ptr] + code_sect;
  597. if ((pc <= code_sect) ||
  598. (pc >= debug_sect))
  599. status = -ERANGE;
  600. }
  601. break;
  602. case OP_CMPS:
  603. /*
  604. * Array short compare
  605. * ...stack 0 is source 1 value
  606. * ...stack 1 is source 2 value
  607. * ...stack 2 is mask value
  608. * ...stack 3 is count
  609. */
  610. if (altera_check_stack(stack_ptr, 4, &status)) {
  611. s32 a = stack[--stack_ptr];
  612. s32 b = stack[--stack_ptr];
  613. long_tmp = stack[--stack_ptr];
  614. count = stack[stack_ptr - 1];
  615. if ((count < 1) || (count > 32))
  616. status = -ERANGE;
  617. else {
  618. long_tmp &= ((-1L) >> (32 - count));
  619. stack[stack_ptr - 1] =
  620. ((a & long_tmp) == (b & long_tmp))
  621. ? 1L : 0L;
  622. }
  623. }
  624. break;
  625. case OP_PINT:
  626. /*
  627. * PRINT add integer
  628. * ...stack 0 is integer value
  629. */
  630. if (!altera_check_stack(stack_ptr, 1, &status))
  631. break;
  632. sprintf(&msg_buff[strlen(msg_buff)],
  633. "%ld", stack[--stack_ptr]);
  634. break;
  635. case OP_PRNT:
  636. /* PRINT finish */
  637. if (debug)
  638. printk(msg_buff, "\n");
  639. msg_buff[0] = '\0';
  640. break;
  641. case OP_DSS:
  642. /*
  643. * DRSCAN short
  644. * ...stack 0 is scan data
  645. * ...stack 1 is count
  646. */
  647. if (!altera_check_stack(stack_ptr, 2, &status))
  648. break;
  649. long_tmp = stack[--stack_ptr];
  650. count = stack[--stack_ptr];
  651. put_unaligned_le32(long_tmp, &charbuf[0]);
  652. status = altera_drscan(astate, count, charbuf, 0);
  653. break;
  654. case OP_DSSC:
  655. /*
  656. * DRSCAN short with capture
  657. * ...stack 0 is scan data
  658. * ...stack 1 is count
  659. */
  660. if (!altera_check_stack(stack_ptr, 2, &status))
  661. break;
  662. long_tmp = stack[--stack_ptr];
  663. count = stack[stack_ptr - 1];
  664. put_unaligned_le32(long_tmp, &charbuf[0]);
  665. status = altera_swap_dr(astate, count, charbuf,
  666. 0, charbuf, 0);
  667. stack[stack_ptr - 1] = get_unaligned_le32(&charbuf[0]);
  668. break;
  669. case OP_ISS:
  670. /*
  671. * IRSCAN short
  672. * ...stack 0 is scan data
  673. * ...stack 1 is count
  674. */
  675. if (!altera_check_stack(stack_ptr, 2, &status))
  676. break;
  677. long_tmp = stack[--stack_ptr];
  678. count = stack[--stack_ptr];
  679. put_unaligned_le32(long_tmp, &charbuf[0]);
  680. status = altera_irscan(astate, count, charbuf, 0);
  681. break;
  682. case OP_ISSC:
  683. /*
  684. * IRSCAN short with capture
  685. * ...stack 0 is scan data
  686. * ...stack 1 is count
  687. */
  688. if (!altera_check_stack(stack_ptr, 2, &status))
  689. break;
  690. long_tmp = stack[--stack_ptr];
  691. count = stack[stack_ptr - 1];
  692. put_unaligned_le32(long_tmp, &charbuf[0]);
  693. status = altera_swap_ir(astate, count, charbuf,
  694. 0, charbuf, 0);
  695. stack[stack_ptr - 1] = get_unaligned_le32(&charbuf[0]);
  696. break;
  697. case OP_DPR:
  698. if (!altera_check_stack(stack_ptr, 1, &status))
  699. break;
  700. count = stack[--stack_ptr];
  701. status = altera_set_dr_pre(&astate->js, count, 0, NULL);
  702. break;
  703. case OP_DPRL:
  704. /*
  705. * DRPRE with literal data
  706. * ...stack 0 is count
  707. * ...stack 1 is literal data
  708. */
  709. if (!altera_check_stack(stack_ptr, 2, &status))
  710. break;
  711. count = stack[--stack_ptr];
  712. long_tmp = stack[--stack_ptr];
  713. put_unaligned_le32(long_tmp, &charbuf[0]);
  714. status = altera_set_dr_pre(&astate->js, count, 0,
  715. charbuf);
  716. break;
  717. case OP_DPO:
  718. /*
  719. * DRPOST
  720. * ...stack 0 is count
  721. */
  722. if (altera_check_stack(stack_ptr, 1, &status)) {
  723. count = stack[--stack_ptr];
  724. status = altera_set_dr_post(&astate->js, count,
  725. 0, NULL);
  726. }
  727. break;
  728. case OP_DPOL:
  729. /*
  730. * DRPOST with literal data
  731. * ...stack 0 is count
  732. * ...stack 1 is literal data
  733. */
  734. if (!altera_check_stack(stack_ptr, 2, &status))
  735. break;
  736. count = stack[--stack_ptr];
  737. long_tmp = stack[--stack_ptr];
  738. put_unaligned_le32(long_tmp, &charbuf[0]);
  739. status = altera_set_dr_post(&astate->js, count, 0,
  740. charbuf);
  741. break;
  742. case OP_IPR:
  743. if (altera_check_stack(stack_ptr, 1, &status)) {
  744. count = stack[--stack_ptr];
  745. status = altera_set_ir_pre(&astate->js, count,
  746. 0, NULL);
  747. }
  748. break;
  749. case OP_IPRL:
  750. /*
  751. * IRPRE with literal data
  752. * ...stack 0 is count
  753. * ...stack 1 is literal data
  754. */
  755. if (altera_check_stack(stack_ptr, 2, &status)) {
  756. count = stack[--stack_ptr];
  757. long_tmp = stack[--stack_ptr];
  758. put_unaligned_le32(long_tmp, &charbuf[0]);
  759. status = altera_set_ir_pre(&astate->js, count,
  760. 0, charbuf);
  761. }
  762. break;
  763. case OP_IPO:
  764. /*
  765. * IRPOST
  766. * ...stack 0 is count
  767. */
  768. if (altera_check_stack(stack_ptr, 1, &status)) {
  769. count = stack[--stack_ptr];
  770. status = altera_set_ir_post(&astate->js, count,
  771. 0, NULL);
  772. }
  773. break;
  774. case OP_IPOL:
  775. /*
  776. * IRPOST with literal data
  777. * ...stack 0 is count
  778. * ...stack 1 is literal data
  779. */
  780. if (!altera_check_stack(stack_ptr, 2, &status))
  781. break;
  782. count = stack[--stack_ptr];
  783. long_tmp = stack[--stack_ptr];
  784. put_unaligned_le32(long_tmp, &charbuf[0]);
  785. status = altera_set_ir_post(&astate->js, count, 0,
  786. charbuf);
  787. break;
  788. case OP_PCHR:
  789. if (altera_check_stack(stack_ptr, 1, &status)) {
  790. u8 ch;
  791. count = strlen(msg_buff);
  792. ch = (char) stack[--stack_ptr];
  793. if ((ch < 1) || (ch > 127)) {
  794. /*
  795. * character code out of range
  796. * instead of flagging an error,
  797. * force the value to 127
  798. */
  799. ch = 127;
  800. }
  801. msg_buff[count] = ch;
  802. msg_buff[count + 1] = '\0';
  803. }
  804. break;
  805. case OP_EXIT:
  806. if (altera_check_stack(stack_ptr, 1, &status))
  807. *exit_code = stack[--stack_ptr];
  808. done = 1;
  809. break;
  810. case OP_EQU:
  811. if (!altera_check_stack(stack_ptr, 2, &status))
  812. break;
  813. --stack_ptr;
  814. stack[stack_ptr - 1] =
  815. (stack[stack_ptr - 1] == stack[stack_ptr]) ?
  816. 1L : 0L;
  817. break;
  818. case OP_POPT:
  819. if (altera_check_stack(stack_ptr, 1, &status))
  820. --stack_ptr;
  821. break;
  822. case OP_ABS:
  823. if (!altera_check_stack(stack_ptr, 1, &status))
  824. break;
  825. if (stack[stack_ptr - 1] < 0)
  826. stack[stack_ptr - 1] = 0 - stack[stack_ptr - 1];
  827. break;
  828. case OP_BCH0:
  829. /*
  830. * Batch operation 0
  831. * SWP
  832. * SWPN 7
  833. * SWP
  834. * SWPN 6
  835. * DUPN 8
  836. * SWPN 2
  837. * SWP
  838. * DUPN 6
  839. * DUPN 6
  840. */
  841. /* SWP */
  842. if (altera_check_stack(stack_ptr, 2, &status)) {
  843. long_tmp = stack[stack_ptr - 2];
  844. stack[stack_ptr - 2] = stack[stack_ptr - 1];
  845. stack[stack_ptr - 1] = long_tmp;
  846. }
  847. /* SWPN 7 */
  848. index = 7 + 1;
  849. if (altera_check_stack(stack_ptr, index, &status)) {
  850. long_tmp = stack[stack_ptr - index];
  851. stack[stack_ptr - index] = stack[stack_ptr - 1];
  852. stack[stack_ptr - 1] = long_tmp;
  853. }
  854. /* SWP */
  855. if (altera_check_stack(stack_ptr, 2, &status)) {
  856. long_tmp = stack[stack_ptr - 2];
  857. stack[stack_ptr - 2] = stack[stack_ptr - 1];
  858. stack[stack_ptr - 1] = long_tmp;
  859. }
  860. /* SWPN 6 */
  861. index = 6 + 1;
  862. if (altera_check_stack(stack_ptr, index, &status)) {
  863. long_tmp = stack[stack_ptr - index];
  864. stack[stack_ptr - index] = stack[stack_ptr - 1];
  865. stack[stack_ptr - 1] = long_tmp;
  866. }
  867. /* DUPN 8 */
  868. index = 8 + 1;
  869. if (altera_check_stack(stack_ptr, index, &status)) {
  870. stack[stack_ptr] = stack[stack_ptr - index];
  871. ++stack_ptr;
  872. }
  873. /* SWPN 2 */
  874. index = 2 + 1;
  875. if (altera_check_stack(stack_ptr, index, &status)) {
  876. long_tmp = stack[stack_ptr - index];
  877. stack[stack_ptr - index] = stack[stack_ptr - 1];
  878. stack[stack_ptr - 1] = long_tmp;
  879. }
  880. /* SWP */
  881. if (altera_check_stack(stack_ptr, 2, &status)) {
  882. long_tmp = stack[stack_ptr - 2];
  883. stack[stack_ptr - 2] = stack[stack_ptr - 1];
  884. stack[stack_ptr - 1] = long_tmp;
  885. }
  886. /* DUPN 6 */
  887. index = 6 + 1;
  888. if (altera_check_stack(stack_ptr, index, &status)) {
  889. stack[stack_ptr] = stack[stack_ptr - index];
  890. ++stack_ptr;
  891. }
  892. /* DUPN 6 */
  893. index = 6 + 1;
  894. if (altera_check_stack(stack_ptr, index, &status)) {
  895. stack[stack_ptr] = stack[stack_ptr - index];
  896. ++stack_ptr;
  897. }
  898. break;
  899. case OP_PSH0:
  900. stack[stack_ptr++] = 0;
  901. break;
  902. case OP_PSHL:
  903. stack[stack_ptr++] = (s32) args[0];
  904. break;
  905. case OP_PSHV:
  906. stack[stack_ptr++] = vars[args[0]];
  907. break;
  908. case OP_JMP:
  909. pc = args[0] + code_sect;
  910. if ((pc < code_sect) || (pc >= debug_sect))
  911. status = -ERANGE;
  912. break;
  913. case OP_CALL:
  914. stack[stack_ptr++] = pc;
  915. pc = args[0] + code_sect;
  916. if ((pc < code_sect) || (pc >= debug_sect))
  917. status = -ERANGE;
  918. break;
  919. case OP_NEXT:
  920. /*
  921. * Process FOR / NEXT loop
  922. * ...argument 0 is variable ID
  923. * ...stack 0 is step value
  924. * ...stack 1 is end value
  925. * ...stack 2 is top address
  926. */
  927. if (altera_check_stack(stack_ptr, 3, &status)) {
  928. s32 step = stack[stack_ptr - 1];
  929. s32 end = stack[stack_ptr - 2];
  930. s32 top = stack[stack_ptr - 3];
  931. s32 iterator = vars[args[0]];
  932. int break_out = 0;
  933. if (step < 0) {
  934. if (iterator <= end)
  935. break_out = 1;
  936. } else if (iterator >= end)
  937. break_out = 1;
  938. if (break_out) {
  939. stack_ptr -= 3;
  940. } else {
  941. vars[args[0]] = iterator + step;
  942. pc = top + code_sect;
  943. if ((pc < code_sect) ||
  944. (pc >= debug_sect))
  945. status = -ERANGE;
  946. }
  947. }
  948. break;
  949. case OP_PSTR:
  950. /*
  951. * PRINT add string
  952. * ...argument 0 is string ID
  953. */
  954. count = strlen(msg_buff);
  955. strlcpy(&msg_buff[count],
  956. &p[str_table + args[0]],
  957. ALTERA_MESSAGE_LENGTH - count);
  958. break;
  959. case OP_SINT:
  960. /*
  961. * STATE intermediate state
  962. * ...argument 0 is state code
  963. */
  964. status = altera_goto_jstate(astate, args[0]);
  965. break;
  966. case OP_ST:
  967. /*
  968. * STATE final state
  969. * ...argument 0 is state code
  970. */
  971. status = altera_goto_jstate(astate, args[0]);
  972. break;
  973. case OP_ISTP:
  974. /*
  975. * IRSTOP state
  976. * ...argument 0 is state code
  977. */
  978. status = altera_set_irstop(&astate->js, args[0]);
  979. break;
  980. case OP_DSTP:
  981. /*
  982. * DRSTOP state
  983. * ...argument 0 is state code
  984. */
  985. status = altera_set_drstop(&astate->js, args[0]);
  986. break;
  987. case OP_SWPN:
  988. /*
  989. * Exchange top with Nth stack value
  990. * ...argument 0 is 0-based stack entry
  991. * to swap with top element
  992. */
  993. index = (args[0]) + 1;
  994. if (altera_check_stack(stack_ptr, index, &status)) {
  995. long_tmp = stack[stack_ptr - index];
  996. stack[stack_ptr - index] = stack[stack_ptr - 1];
  997. stack[stack_ptr - 1] = long_tmp;
  998. }
  999. break;
  1000. case OP_DUPN:
  1001. /*
  1002. * Duplicate Nth stack value
  1003. * ...argument 0 is 0-based stack entry to duplicate
  1004. */
  1005. index = (args[0]) + 1;
  1006. if (altera_check_stack(stack_ptr, index, &status)) {
  1007. stack[stack_ptr] = stack[stack_ptr - index];
  1008. ++stack_ptr;
  1009. }
  1010. break;
  1011. case OP_POPV:
  1012. /*
  1013. * Pop stack into scalar variable
  1014. * ...argument 0 is variable ID
  1015. * ...stack 0 is value
  1016. */
  1017. if (altera_check_stack(stack_ptr, 1, &status))
  1018. vars[args[0]] = stack[--stack_ptr];
  1019. break;
  1020. case OP_POPE:
  1021. /*
  1022. * Pop stack into integer array element
  1023. * ...argument 0 is variable ID
  1024. * ...stack 0 is array index
  1025. * ...stack 1 is value
  1026. */
  1027. if (!altera_check_stack(stack_ptr, 2, &status))
  1028. break;
  1029. variable_id = args[0];
  1030. /*
  1031. * If variable is read-only,
  1032. * convert to writable array
  1033. */
  1034. if ((version > 0) &&
  1035. ((attrs[variable_id] & 0x9c) == 0x1c)) {
  1036. /* Allocate a writable buffer for this array */
  1037. count = var_size[variable_id];
  1038. long_tmp = vars[variable_id];
  1039. longptr_tmp = kzalloc(count * sizeof(long),
  1040. GFP_KERNEL);
  1041. vars[variable_id] = (long)longptr_tmp;
  1042. if (vars[variable_id] == 0) {
  1043. status = -ENOMEM;
  1044. break;
  1045. }
  1046. /* copy previous contents into buffer */
  1047. for (i = 0; i < count; ++i) {
  1048. longptr_tmp[i] =
  1049. get_unaligned_be32(&p[long_tmp]);
  1050. long_tmp += sizeof(long);
  1051. }
  1052. /*
  1053. * set bit 7 - buffer was
  1054. * dynamically allocated
  1055. */
  1056. attrs[variable_id] |= 0x80;
  1057. /* clear bit 2 - variable is writable */
  1058. attrs[variable_id] &= ~0x04;
  1059. attrs[variable_id] |= 0x01;
  1060. }
  1061. /* check that variable is a writable integer array */
  1062. if ((attrs[variable_id] & 0x1c) != 0x18)
  1063. status = -ERANGE;
  1064. else {
  1065. longptr_tmp = (long *)vars[variable_id];
  1066. /* pop the array index */
  1067. index = stack[--stack_ptr];
  1068. /* pop the value and store it into the array */
  1069. longptr_tmp[index] = stack[--stack_ptr];
  1070. }
  1071. break;
  1072. case OP_POPA:
  1073. /*
  1074. * Pop stack into Boolean array
  1075. * ...argument 0 is variable ID
  1076. * ...stack 0 is count
  1077. * ...stack 1 is array index
  1078. * ...stack 2 is value
  1079. */
  1080. if (!altera_check_stack(stack_ptr, 3, &status))
  1081. break;
  1082. variable_id = args[0];
  1083. /*
  1084. * If variable is read-only,
  1085. * convert to writable array
  1086. */
  1087. if ((version > 0) &&
  1088. ((attrs[variable_id] & 0x9c) == 0x0c)) {
  1089. /* Allocate a writable buffer for this array */
  1090. long_tmp =
  1091. (var_size[variable_id] + 7L) >> 3L;
  1092. charptr_tmp2 = (u8 *)vars[variable_id];
  1093. charptr_tmp =
  1094. kzalloc(long_tmp, GFP_KERNEL);
  1095. vars[variable_id] = (long)charptr_tmp;
  1096. if (vars[variable_id] == 0) {
  1097. status = -ENOMEM;
  1098. break;
  1099. }
  1100. /* zero the buffer */
  1101. for (long_idx = 0L;
  1102. long_idx < long_tmp;
  1103. ++long_idx) {
  1104. charptr_tmp[long_idx] = 0;
  1105. }
  1106. /* copy previous contents into buffer */
  1107. for (long_idx = 0L;
  1108. long_idx < var_size[variable_id];
  1109. ++long_idx) {
  1110. long_idx2 = long_idx;
  1111. if (charptr_tmp2[long_idx2 >> 3] &
  1112. (1 << (long_idx2 & 7))) {
  1113. charptr_tmp[long_idx >> 3] |=
  1114. (1 << (long_idx & 7));
  1115. }
  1116. }
  1117. /*
  1118. * set bit 7 - buffer was
  1119. * dynamically allocated
  1120. */
  1121. attrs[variable_id] |= 0x80;
  1122. /* clear bit 2 - variable is writable */
  1123. attrs[variable_id] &= ~0x04;
  1124. attrs[variable_id] |= 0x01;
  1125. }
  1126. /*
  1127. * check that variable is
  1128. * a writable Boolean array
  1129. */
  1130. if ((attrs[variable_id] & 0x1c) != 0x08) {
  1131. status = -ERANGE;
  1132. break;
  1133. }
  1134. charptr_tmp = (u8 *)vars[variable_id];
  1135. /* pop the count (number of bits to copy) */
  1136. long_count = stack[--stack_ptr];
  1137. /* pop the array index */
  1138. long_idx = stack[--stack_ptr];
  1139. reverse = 0;
  1140. if (version > 0) {
  1141. /*
  1142. * stack 0 = array right index
  1143. * stack 1 = array left index
  1144. */
  1145. if (long_idx > long_count) {
  1146. reverse = 1;
  1147. long_tmp = long_count;
  1148. long_count = 1 + long_idx -
  1149. long_count;
  1150. long_idx = long_tmp;
  1151. /* reverse POPA is not supported */
  1152. status = -ERANGE;
  1153. break;
  1154. } else
  1155. long_count = 1 + long_count -
  1156. long_idx;
  1157. }
  1158. /* pop the data */
  1159. long_tmp = stack[--stack_ptr];
  1160. if (long_count < 1) {
  1161. status = -ERANGE;
  1162. break;
  1163. }
  1164. for (i = 0; i < long_count; ++i) {
  1165. if (long_tmp & (1L << (s32) i))
  1166. charptr_tmp[long_idx >> 3L] |=
  1167. (1L << (long_idx & 7L));
  1168. else
  1169. charptr_tmp[long_idx >> 3L] &=
  1170. ~(1L << (long_idx & 7L));
  1171. ++long_idx;
  1172. }
  1173. break;
  1174. case OP_JMPZ:
  1175. /*
  1176. * Pop stack and branch if zero
  1177. * ...argument 0 is address
  1178. * ...stack 0 is condition value
  1179. */
  1180. if (altera_check_stack(stack_ptr, 1, &status)) {
  1181. if (stack[--stack_ptr] == 0) {
  1182. pc = args[0] + code_sect;
  1183. if ((pc < code_sect) ||
  1184. (pc >= debug_sect))
  1185. status = -ERANGE;
  1186. }
  1187. }
  1188. break;
  1189. case OP_DS:
  1190. case OP_IS:
  1191. /*
  1192. * DRSCAN
  1193. * IRSCAN
  1194. * ...argument 0 is scan data variable ID
  1195. * ...stack 0 is array index
  1196. * ...stack 1 is count
  1197. */
  1198. if (!altera_check_stack(stack_ptr, 2, &status))
  1199. break;
  1200. long_idx = stack[--stack_ptr];
  1201. long_count = stack[--stack_ptr];
  1202. reverse = 0;
  1203. if (version > 0) {
  1204. /*
  1205. * stack 0 = array right index
  1206. * stack 1 = array left index
  1207. * stack 2 = count
  1208. */
  1209. long_tmp = long_count;
  1210. long_count = stack[--stack_ptr];
  1211. if (long_idx > long_tmp) {
  1212. reverse = 1;
  1213. long_idx = long_tmp;
  1214. }
  1215. }
  1216. charptr_tmp = (u8 *)vars[args[0]];
  1217. if (reverse) {
  1218. /*
  1219. * allocate a buffer
  1220. * and reverse the data order
  1221. */
  1222. charptr_tmp2 = charptr_tmp;
  1223. charptr_tmp = kzalloc((long_count >> 3) + 1,
  1224. GFP_KERNEL);
  1225. if (charptr_tmp == NULL) {
  1226. status = -ENOMEM;
  1227. break;
  1228. }
  1229. long_tmp = long_idx + long_count - 1;
  1230. long_idx2 = 0;
  1231. while (long_idx2 < long_count) {
  1232. if (charptr_tmp2[long_tmp >> 3] &
  1233. (1 << (long_tmp & 7)))
  1234. charptr_tmp[long_idx2 >> 3] |=
  1235. (1 << (long_idx2 & 7));
  1236. else
  1237. charptr_tmp[long_idx2 >> 3] &=
  1238. ~(1 << (long_idx2 & 7));
  1239. --long_tmp;
  1240. ++long_idx2;
  1241. }
  1242. }
  1243. if (opcode == 0x51) /* DS */
  1244. status = altera_drscan(astate, long_count,
  1245. charptr_tmp, long_idx);
  1246. else /* IS */
  1247. status = altera_irscan(astate, long_count,
  1248. charptr_tmp, long_idx);
  1249. if (reverse)
  1250. kfree(charptr_tmp);
  1251. break;
  1252. case OP_DPRA:
  1253. /*
  1254. * DRPRE with array data
  1255. * ...argument 0 is variable ID
  1256. * ...stack 0 is array index
  1257. * ...stack 1 is count
  1258. */
  1259. if (!altera_check_stack(stack_ptr, 2, &status))
  1260. break;
  1261. index = stack[--stack_ptr];
  1262. count = stack[--stack_ptr];
  1263. if (version > 0)
  1264. /*
  1265. * stack 0 = array right index
  1266. * stack 1 = array left index
  1267. */
  1268. count = 1 + count - index;
  1269. charptr_tmp = (u8 *)vars[args[0]];
  1270. status = altera_set_dr_pre(&astate->js, count, index,
  1271. charptr_tmp);
  1272. break;
  1273. case OP_DPOA:
  1274. /*
  1275. * DRPOST with array data
  1276. * ...argument 0 is variable ID
  1277. * ...stack 0 is array index
  1278. * ...stack 1 is count
  1279. */
  1280. if (!altera_check_stack(stack_ptr, 2, &status))
  1281. break;
  1282. index = stack[--stack_ptr];
  1283. count = stack[--stack_ptr];
  1284. if (version > 0)
  1285. /*
  1286. * stack 0 = array right index
  1287. * stack 1 = array left index
  1288. */
  1289. count = 1 + count - index;
  1290. charptr_tmp = (u8 *)vars[args[0]];
  1291. status = altera_set_dr_post(&astate->js, count, index,
  1292. charptr_tmp);
  1293. break;
  1294. case OP_IPRA:
  1295. /*
  1296. * IRPRE with array data
  1297. * ...argument 0 is variable ID
  1298. * ...stack 0 is array index
  1299. * ...stack 1 is count
  1300. */
  1301. if (!altera_check_stack(stack_ptr, 2, &status))
  1302. break;
  1303. index = stack[--stack_ptr];
  1304. count = stack[--stack_ptr];
  1305. if (version > 0)
  1306. /*
  1307. * stack 0 = array right index
  1308. * stack 1 = array left index
  1309. */
  1310. count = 1 + count - index;
  1311. charptr_tmp = (u8 *)vars[args[0]];
  1312. status = altera_set_ir_pre(&astate->js, count, index,
  1313. charptr_tmp);
  1314. break;
  1315. case OP_IPOA:
  1316. /*
  1317. * IRPOST with array data
  1318. * ...argument 0 is variable ID
  1319. * ...stack 0 is array index
  1320. * ...stack 1 is count
  1321. */
  1322. if (!altera_check_stack(stack_ptr, 2, &status))
  1323. break;
  1324. index = stack[--stack_ptr];
  1325. count = stack[--stack_ptr];
  1326. if (version > 0)
  1327. /*
  1328. * stack 0 = array right index
  1329. * stack 1 = array left index
  1330. */
  1331. count = 1 + count - index;
  1332. charptr_tmp = (u8 *)vars[args[0]];
  1333. status = altera_set_ir_post(&astate->js, count, index,
  1334. charptr_tmp);
  1335. break;
  1336. case OP_EXPT:
  1337. /*
  1338. * EXPORT
  1339. * ...argument 0 is string ID
  1340. * ...stack 0 is integer expression
  1341. */
  1342. if (altera_check_stack(stack_ptr, 1, &status)) {
  1343. name = &p[str_table + args[0]];
  1344. long_tmp = stack[--stack_ptr];
  1345. altera_export_int(name, long_tmp);
  1346. }
  1347. break;
  1348. case OP_PSHE:
  1349. /*
  1350. * Push integer array element
  1351. * ...argument 0 is variable ID
  1352. * ...stack 0 is array index
  1353. */
  1354. if (!altera_check_stack(stack_ptr, 1, &status))
  1355. break;
  1356. variable_id = args[0];
  1357. index = stack[stack_ptr - 1];
  1358. /* check variable type */
  1359. if ((attrs[variable_id] & 0x1f) == 0x19) {
  1360. /* writable integer array */
  1361. longptr_tmp = (long *)vars[variable_id];
  1362. stack[stack_ptr - 1] = longptr_tmp[index];
  1363. } else if ((attrs[variable_id] & 0x1f) == 0x1c) {
  1364. /* read-only integer array */
  1365. long_tmp = vars[variable_id] +
  1366. (index * sizeof(long));
  1367. stack[stack_ptr - 1] =
  1368. get_unaligned_be32(&p[long_tmp]);
  1369. } else
  1370. status = -ERANGE;
  1371. break;
  1372. case OP_PSHA:
  1373. /*
  1374. * Push Boolean array
  1375. * ...argument 0 is variable ID
  1376. * ...stack 0 is count
  1377. * ...stack 1 is array index
  1378. */
  1379. if (!altera_check_stack(stack_ptr, 2, &status))
  1380. break;
  1381. variable_id = args[0];
  1382. /* check that variable is a Boolean array */
  1383. if ((attrs[variable_id] & 0x18) != 0x08) {
  1384. status = -ERANGE;
  1385. break;
  1386. }
  1387. charptr_tmp = (u8 *)vars[variable_id];
  1388. /* pop the count (number of bits to copy) */
  1389. count = stack[--stack_ptr];
  1390. /* pop the array index */
  1391. index = stack[stack_ptr - 1];
  1392. if (version > 0)
  1393. /*
  1394. * stack 0 = array right index
  1395. * stack 1 = array left index
  1396. */
  1397. count = 1 + count - index;
  1398. if ((count < 1) || (count > 32)) {
  1399. status = -ERANGE;
  1400. break;
  1401. }
  1402. long_tmp = 0L;
  1403. for (i = 0; i < count; ++i)
  1404. if (charptr_tmp[(i + index) >> 3] &
  1405. (1 << ((i + index) & 7)))
  1406. long_tmp |= (1L << i);
  1407. stack[stack_ptr - 1] = long_tmp;
  1408. break;
  1409. case OP_DYNA:
  1410. /*
  1411. * Dynamically change size of array
  1412. * ...argument 0 is variable ID
  1413. * ...stack 0 is new size
  1414. */
  1415. if (!altera_check_stack(stack_ptr, 1, &status))
  1416. break;
  1417. variable_id = args[0];
  1418. long_tmp = stack[--stack_ptr];
  1419. if (long_tmp > var_size[variable_id]) {
  1420. var_size[variable_id] = long_tmp;
  1421. if (attrs[variable_id] & 0x10)
  1422. /* allocate integer array */
  1423. long_tmp *= sizeof(long);
  1424. else
  1425. /* allocate Boolean array */
  1426. long_tmp = (long_tmp + 7) >> 3;
  1427. /*
  1428. * If the buffer was previously allocated,
  1429. * free it
  1430. */
  1431. if (attrs[variable_id] & 0x80) {
  1432. kfree((void *)vars[variable_id]);
  1433. vars[variable_id] = 0;
  1434. }
  1435. /*
  1436. * Allocate a new buffer
  1437. * of the requested size
  1438. */
  1439. vars[variable_id] = (long)
  1440. kzalloc(long_tmp, GFP_KERNEL);
  1441. if (vars[variable_id] == 0) {
  1442. status = -ENOMEM;
  1443. break;
  1444. }
  1445. /*
  1446. * Set the attribute bit to indicate that
  1447. * this buffer was dynamically allocated and
  1448. * should be freed later
  1449. */
  1450. attrs[variable_id] |= 0x80;
  1451. /* zero out memory */
  1452. count = ((var_size[variable_id] + 7L) /
  1453. 8L);
  1454. charptr_tmp = (u8 *)(vars[variable_id]);
  1455. for (index = 0; index < count; ++index)
  1456. charptr_tmp[index] = 0;
  1457. }
  1458. break;
  1459. case OP_EXPV:
  1460. /*
  1461. * Export Boolean array
  1462. * ...argument 0 is string ID
  1463. * ...stack 0 is variable ID
  1464. * ...stack 1 is array right index
  1465. * ...stack 2 is array left index
  1466. */
  1467. if (!altera_check_stack(stack_ptr, 3, &status))
  1468. break;
  1469. if (version == 0) {
  1470. /* EXPV is not supported in JBC 1.0 */
  1471. bad_opcode = 1;
  1472. break;
  1473. }
  1474. name = &p[str_table + args[0]];
  1475. variable_id = stack[--stack_ptr];
  1476. long_idx = stack[--stack_ptr];/* right indx */
  1477. long_idx2 = stack[--stack_ptr];/* left indx */
  1478. if (long_idx > long_idx2) {
  1479. /* reverse indices not supported */
  1480. status = -ERANGE;
  1481. break;
  1482. }
  1483. long_count = 1 + long_idx2 - long_idx;
  1484. charptr_tmp = (u8 *)vars[variable_id];
  1485. charptr_tmp2 = NULL;
  1486. if ((long_idx & 7L) != 0) {
  1487. s32 k = long_idx;
  1488. charptr_tmp2 =
  1489. kzalloc(((long_count + 7L) / 8L),
  1490. GFP_KERNEL);
  1491. if (charptr_tmp2 == NULL) {
  1492. status = -ENOMEM;
  1493. break;
  1494. }
  1495. for (i = 0; i < long_count; ++i) {
  1496. if (charptr_tmp[k >> 3] &
  1497. (1 << (k & 7)))
  1498. charptr_tmp2[i >> 3] |=
  1499. (1 << (i & 7));
  1500. else
  1501. charptr_tmp2[i >> 3] &=
  1502. ~(1 << (i & 7));
  1503. ++k;
  1504. }
  1505. charptr_tmp = charptr_tmp2;
  1506. } else if (long_idx != 0)
  1507. charptr_tmp = &charptr_tmp[long_idx >> 3];
  1508. altera_export_bool_array(name, charptr_tmp,
  1509. long_count);
  1510. /* free allocated buffer */
  1511. if ((long_idx & 7L) != 0)
  1512. kfree(charptr_tmp2);
  1513. break;
  1514. case OP_COPY: {
  1515. /*
  1516. * Array copy
  1517. * ...argument 0 is dest ID
  1518. * ...argument 1 is source ID
  1519. * ...stack 0 is count
  1520. * ...stack 1 is dest index
  1521. * ...stack 2 is source index
  1522. */
  1523. s32 copy_count;
  1524. s32 copy_index;
  1525. s32 copy_index2;
  1526. s32 destleft;
  1527. s32 src_count;
  1528. s32 dest_count;
  1529. int src_reverse = 0;
  1530. int dest_reverse = 0;
  1531. if (!altera_check_stack(stack_ptr, 3, &status))
  1532. break;
  1533. copy_count = stack[--stack_ptr];
  1534. copy_index = stack[--stack_ptr];
  1535. copy_index2 = stack[--stack_ptr];
  1536. reverse = 0;
  1537. if (version > 0) {
  1538. /*
  1539. * stack 0 = source right index
  1540. * stack 1 = source left index
  1541. * stack 2 = destination right index
  1542. * stack 3 = destination left index
  1543. */
  1544. destleft = stack[--stack_ptr];
  1545. if (copy_count > copy_index) {
  1546. src_reverse = 1;
  1547. reverse = 1;
  1548. src_count = 1 + copy_count - copy_index;
  1549. /* copy_index = source start index */
  1550. } else {
  1551. src_count = 1 + copy_index - copy_count;
  1552. /* source start index */
  1553. copy_index = copy_count;
  1554. }
  1555. if (copy_index2 > destleft) {
  1556. dest_reverse = 1;
  1557. reverse = !reverse;
  1558. dest_count = 1 + copy_index2 - destleft;
  1559. /* destination start index */
  1560. copy_index2 = destleft;
  1561. } else
  1562. dest_count = 1 + destleft - copy_index2;
  1563. copy_count = (src_count < dest_count) ?
  1564. src_count : dest_count;
  1565. if ((src_reverse || dest_reverse) &&
  1566. (src_count != dest_count))
  1567. /*
  1568. * If either the source or destination
  1569. * is reversed, we can't tolerate
  1570. * a length mismatch, because we
  1571. * "left justify" arrays when copying.
  1572. * This won't work correctly
  1573. * with reversed arrays.
  1574. */
  1575. status = -ERANGE;
  1576. }
  1577. count = copy_count;
  1578. index = copy_index;
  1579. index2 = copy_index2;
  1580. /*
  1581. * If destination is a read-only array,
  1582. * allocate a buffer and convert it to a writable array
  1583. */
  1584. variable_id = args[1];
  1585. if ((version > 0) &&
  1586. ((attrs[variable_id] & 0x9c) == 0x0c)) {
  1587. /* Allocate a writable buffer for this array */
  1588. long_tmp =
  1589. (var_size[variable_id] + 7L) >> 3L;
  1590. charptr_tmp2 = (u8 *)vars[variable_id];
  1591. charptr_tmp =
  1592. kzalloc(long_tmp, GFP_KERNEL);
  1593. vars[variable_id] = (long)charptr_tmp;
  1594. if (vars[variable_id] == 0) {
  1595. status = -ENOMEM;
  1596. break;
  1597. }
  1598. /* zero the buffer */
  1599. for (long_idx = 0L; long_idx < long_tmp;
  1600. ++long_idx)
  1601. charptr_tmp[long_idx] = 0;
  1602. /* copy previous contents into buffer */
  1603. for (long_idx = 0L;
  1604. long_idx < var_size[variable_id];
  1605. ++long_idx) {
  1606. long_idx2 = long_idx;
  1607. if (charptr_tmp2[long_idx2 >> 3] &
  1608. (1 << (long_idx2 & 7)))
  1609. charptr_tmp[long_idx >> 3] |=
  1610. (1 << (long_idx & 7));
  1611. }
  1612. /*
  1613. set bit 7 - buffer was dynamically allocated */
  1614. attrs[variable_id] |= 0x80;
  1615. /* clear bit 2 - variable is writable */
  1616. attrs[variable_id] &= ~0x04;
  1617. attrs[variable_id] |= 0x01;
  1618. }
  1619. charptr_tmp = (u8 *)vars[args[1]];
  1620. charptr_tmp2 = (u8 *)vars[args[0]];
  1621. /* check if destination is a writable Boolean array */
  1622. if ((attrs[args[1]] & 0x1c) != 0x08) {
  1623. status = -ERANGE;
  1624. break;
  1625. }
  1626. if (count < 1) {
  1627. status = -ERANGE;
  1628. break;
  1629. }
  1630. if (reverse)
  1631. index2 += (count - 1);
  1632. for (i = 0; i < count; ++i) {
  1633. if (charptr_tmp2[index >> 3] &
  1634. (1 << (index & 7)))
  1635. charptr_tmp[index2 >> 3] |=
  1636. (1 << (index2 & 7));
  1637. else
  1638. charptr_tmp[index2 >> 3] &=
  1639. ~(1 << (index2 & 7));
  1640. ++index;
  1641. if (reverse)
  1642. --index2;
  1643. else
  1644. ++index2;
  1645. }
  1646. break;
  1647. }
  1648. case OP_DSC:
  1649. case OP_ISC: {
  1650. /*
  1651. * DRSCAN with capture
  1652. * IRSCAN with capture
  1653. * ...argument 0 is scan data variable ID
  1654. * ...argument 1 is capture variable ID
  1655. * ...stack 0 is capture index
  1656. * ...stack 1 is scan data index
  1657. * ...stack 2 is count
  1658. */
  1659. s32 scan_right, scan_left;
  1660. s32 capture_count = 0;
  1661. s32 scan_count = 0;
  1662. s32 capture_index;
  1663. s32 scan_index;
  1664. if (!altera_check_stack(stack_ptr, 3, &status))
  1665. break;
  1666. capture_index = stack[--stack_ptr];
  1667. scan_index = stack[--stack_ptr];
  1668. if (version > 0) {
  1669. /*
  1670. * stack 0 = capture right index
  1671. * stack 1 = capture left index
  1672. * stack 2 = scan right index
  1673. * stack 3 = scan left index
  1674. * stack 4 = count
  1675. */
  1676. scan_right = stack[--stack_ptr];
  1677. scan_left = stack[--stack_ptr];
  1678. capture_count = 1 + scan_index - capture_index;
  1679. scan_count = 1 + scan_left - scan_right;
  1680. scan_index = scan_right;
  1681. }
  1682. long_count = stack[--stack_ptr];
  1683. /*
  1684. * If capture array is read-only, allocate a buffer
  1685. * and convert it to a writable array
  1686. */
  1687. variable_id = args[1];
  1688. if ((version > 0) &&
  1689. ((attrs[variable_id] & 0x9c) == 0x0c)) {
  1690. /* Allocate a writable buffer for this array */
  1691. long_tmp =
  1692. (var_size[variable_id] + 7L) >> 3L;
  1693. charptr_tmp2 = (u8 *)vars[variable_id];
  1694. charptr_tmp =
  1695. kzalloc(long_tmp, GFP_KERNEL);
  1696. vars[variable_id] = (long)charptr_tmp;
  1697. if (vars[variable_id] == 0) {
  1698. status = -ENOMEM;
  1699. break;
  1700. }
  1701. /* zero the buffer */
  1702. for (long_idx = 0L; long_idx < long_tmp;
  1703. ++long_idx)
  1704. charptr_tmp[long_idx] = 0;
  1705. /* copy previous contents into buffer */
  1706. for (long_idx = 0L;
  1707. long_idx < var_size[variable_id];
  1708. ++long_idx) {
  1709. long_idx2 = long_idx;
  1710. if (charptr_tmp2[long_idx2 >> 3] &
  1711. (1 << (long_idx2 & 7)))
  1712. charptr_tmp[long_idx >> 3] |=
  1713. (1 << (long_idx & 7));
  1714. }
  1715. /*
  1716. * set bit 7 - buffer was
  1717. * dynamically allocated
  1718. */
  1719. attrs[variable_id] |= 0x80;
  1720. /* clear bit 2 - variable is writable */
  1721. attrs[variable_id] &= ~0x04;
  1722. attrs[variable_id] |= 0x01;
  1723. }
  1724. charptr_tmp = (u8 *)vars[args[0]];
  1725. charptr_tmp2 = (u8 *)vars[args[1]];
  1726. if ((version > 0) &&
  1727. ((long_count > capture_count) ||
  1728. (long_count > scan_count))) {
  1729. status = -ERANGE;
  1730. break;
  1731. }
  1732. /*
  1733. * check that capture array
  1734. * is a writable Boolean array
  1735. */
  1736. if ((attrs[args[1]] & 0x1c) != 0x08) {
  1737. status = -ERANGE;
  1738. break;
  1739. }
  1740. if (status == 0) {
  1741. if (opcode == 0x82) /* DSC */
  1742. status = altera_swap_dr(astate,
  1743. long_count,
  1744. charptr_tmp,
  1745. scan_index,
  1746. charptr_tmp2,
  1747. capture_index);
  1748. else /* ISC */
  1749. status = altera_swap_ir(astate,
  1750. long_count,
  1751. charptr_tmp,
  1752. scan_index,
  1753. charptr_tmp2,
  1754. capture_index);
  1755. }
  1756. break;
  1757. }
  1758. case OP_WAIT:
  1759. /*
  1760. * WAIT
  1761. * ...argument 0 is wait state
  1762. * ...argument 1 is end state
  1763. * ...stack 0 is cycles
  1764. * ...stack 1 is microseconds
  1765. */
  1766. if (!altera_check_stack(stack_ptr, 2, &status))
  1767. break;
  1768. long_tmp = stack[--stack_ptr];
  1769. if (long_tmp != 0L)
  1770. status = altera_wait_cycles(astate, long_tmp,
  1771. args[0]);
  1772. long_tmp = stack[--stack_ptr];
  1773. if ((status == 0) && (long_tmp != 0L))
  1774. status = altera_wait_msecs(astate,
  1775. long_tmp,
  1776. args[0]);
  1777. if ((status == 0) && (args[1] != args[0]))
  1778. status = altera_goto_jstate(astate,
  1779. args[1]);
  1780. if (version > 0) {
  1781. --stack_ptr; /* throw away MAX cycles */
  1782. --stack_ptr; /* throw away MAX microseconds */
  1783. }
  1784. break;
  1785. case OP_CMPA: {
  1786. /*
  1787. * Array compare
  1788. * ...argument 0 is source 1 ID
  1789. * ...argument 1 is source 2 ID
  1790. * ...argument 2 is mask ID
  1791. * ...stack 0 is source 1 index
  1792. * ...stack 1 is source 2 index
  1793. * ...stack 2 is mask index
  1794. * ...stack 3 is count
  1795. */
  1796. s32 a, b;
  1797. u8 *source1 = (u8 *)vars[args[0]];
  1798. u8 *source2 = (u8 *)vars[args[1]];
  1799. u8 *mask = (u8 *)vars[args[2]];
  1800. u32 index1;
  1801. u32 index2;
  1802. u32 mask_index;
  1803. if (!altera_check_stack(stack_ptr, 4, &status))
  1804. break;
  1805. index1 = stack[--stack_ptr];
  1806. index2 = stack[--stack_ptr];
  1807. mask_index = stack[--stack_ptr];
  1808. long_count = stack[--stack_ptr];
  1809. if (version > 0) {
  1810. /*
  1811. * stack 0 = source 1 right index
  1812. * stack 1 = source 1 left index
  1813. * stack 2 = source 2 right index
  1814. * stack 3 = source 2 left index
  1815. * stack 4 = mask right index
  1816. * stack 5 = mask left index
  1817. */
  1818. s32 mask_right = stack[--stack_ptr];
  1819. s32 mask_left = stack[--stack_ptr];
  1820. /* source 1 count */
  1821. a = 1 + index2 - index1;
  1822. /* source 2 count */
  1823. b = 1 + long_count - mask_index;
  1824. a = (a < b) ? a : b;
  1825. /* mask count */
  1826. b = 1 + mask_left - mask_right;
  1827. a = (a < b) ? a : b;
  1828. /* source 2 start index */
  1829. index2 = mask_index;
  1830. /* mask start index */
  1831. mask_index = mask_right;
  1832. long_count = a;
  1833. }
  1834. long_tmp = 1L;
  1835. if (long_count < 1)
  1836. status = -ERANGE;
  1837. else {
  1838. count = long_count;
  1839. for (i = 0; i < count; ++i) {
  1840. if (mask[mask_index >> 3] &
  1841. (1 << (mask_index & 7))) {
  1842. a = source1[index1 >> 3] &
  1843. (1 << (index1 & 7))
  1844. ? 1 : 0;
  1845. b = source2[index2 >> 3] &
  1846. (1 << (index2 & 7))
  1847. ? 1 : 0;
  1848. if (a != b) /* failure */
  1849. long_tmp = 0L;
  1850. }
  1851. ++index1;
  1852. ++index2;
  1853. ++mask_index;
  1854. }
  1855. }
  1856. stack[stack_ptr++] = long_tmp;
  1857. break;
  1858. }
  1859. default:
  1860. /* Unrecognized opcode -- ERROR! */
  1861. bad_opcode = 1;
  1862. break;
  1863. }
  1864. if (bad_opcode)
  1865. status = -ENOSYS;
  1866. if ((stack_ptr < 0) || (stack_ptr >= ALTERA_STACK_SIZE))
  1867. status = -EOVERFLOW;
  1868. if (status != 0) {
  1869. done = 1;
  1870. *error_address = (s32)(opcode_address - code_sect);
  1871. }
  1872. }
  1873. altera_free_buffers(astate);
  1874. /* Free all dynamically allocated arrays */
  1875. if ((attrs != NULL) && (vars != NULL))
  1876. for (i = 0; i < sym_count; ++i)
  1877. if (attrs[i] & 0x80)
  1878. kfree((void *)vars[i]);
  1879. kfree(vars);
  1880. kfree(var_size);
  1881. kfree(attrs);
  1882. kfree(proc_attributes);
  1883. return status;
  1884. }
  1885. static int altera_get_note(u8 *p, s32 program_size,
  1886. s32 *offset, char *key, char *value, int length)
  1887. /*
  1888. * Gets key and value of NOTE fields in the JBC file.
  1889. * Can be called in two modes: if offset pointer is NULL,
  1890. * then the function searches for note fields which match
  1891. * the key string provided. If offset is not NULL, then
  1892. * the function finds the next note field of any key,
  1893. * starting at the offset specified by the offset pointer.
  1894. * Returns 0 for success, else appropriate error code
  1895. */
  1896. {
  1897. int status = -ENODATA;
  1898. u32 note_strings = 0L;
  1899. u32 note_table = 0L;
  1900. u32 note_count = 0L;
  1901. u32 first_word = 0L;
  1902. int version = 0;
  1903. int delta = 0;
  1904. char *key_ptr;
  1905. char *value_ptr;
  1906. int i;
  1907. /* Read header information */
  1908. if (program_size > 52L) {
  1909. first_word = get_unaligned_be32(&p[0]);
  1910. version = (first_word & 1L);
  1911. delta = version * 8;
  1912. note_strings = get_unaligned_be32(&p[8 + delta]);
  1913. note_table = get_unaligned_be32(&p[12 + delta]);
  1914. note_count = get_unaligned_be32(&p[44 + (2 * delta)]);
  1915. }
  1916. if ((first_word != 0x4A414D00L) && (first_word != 0x4A414D01L))
  1917. return -EIO;
  1918. if (note_count <= 0L)
  1919. return status;
  1920. if (offset == NULL) {
  1921. /*
  1922. * We will search for the first note with a specific key,
  1923. * and return only the value
  1924. */
  1925. for (i = 0; (i < note_count) &&
  1926. (status != 0); ++i) {
  1927. key_ptr = &p[note_strings +
  1928. get_unaligned_be32(
  1929. &p[note_table + (8 * i)])];
  1930. if ((strnicmp(key, key_ptr, strlen(key_ptr)) == 0) &&
  1931. (key != NULL)) {
  1932. status = 0;
  1933. value_ptr = &p[note_strings +
  1934. get_unaligned_be32(
  1935. &p[note_table + (8 * i) + 4])];
  1936. if (value != NULL)
  1937. strlcpy(value, value_ptr, length);
  1938. }
  1939. }
  1940. } else {
  1941. /*
  1942. * We will search for the next note, regardless of the key,
  1943. * and return both the value and the key
  1944. */
  1945. i = *offset;
  1946. if ((i >= 0) && (i < note_count)) {
  1947. status = 0;
  1948. if (key != NULL)
  1949. strlcpy(key, &p[note_strings +
  1950. get_unaligned_be32(
  1951. &p[note_table + (8 * i)])],
  1952. length);
  1953. if (value != NULL)
  1954. strlcpy(value, &p[note_strings +
  1955. get_unaligned_be32(
  1956. &p[note_table + (8 * i) + 4])],
  1957. length);
  1958. *offset = i + 1;
  1959. }
  1960. }
  1961. return status;
  1962. }
  1963. static int altera_check_crc(u8 *p, s32 program_size)
  1964. {
  1965. int status = 0;
  1966. u16 local_expected = 0,
  1967. local_actual = 0,
  1968. shift_reg = 0xffff;
  1969. int bit, feedback;
  1970. u8 databyte;
  1971. u32 i;
  1972. u32 crc_section = 0L;
  1973. u32 first_word = 0L;
  1974. int version = 0;
  1975. int delta = 0;
  1976. if (program_size > 52L) {
  1977. first_word = get_unaligned_be32(&p[0]);
  1978. version = (first_word & 1L);
  1979. delta = version * 8;
  1980. crc_section = get_unaligned_be32(&p[32 + delta]);
  1981. }
  1982. if ((first_word != 0x4A414D00L) && (first_word != 0x4A414D01L))
  1983. status = -EIO;
  1984. if (crc_section >= program_size)
  1985. status = -EIO;
  1986. if (status == 0) {
  1987. local_expected = (u16)get_unaligned_be16(&p[crc_section]);
  1988. for (i = 0; i < crc_section; ++i) {
  1989. databyte = p[i];
  1990. for (bit = 0; bit < 8; bit++) {
  1991. feedback = (databyte ^ shift_reg) & 0x01;
  1992. shift_reg >>= 1;
  1993. if (feedback)
  1994. shift_reg ^= 0x8408;
  1995. databyte >>= 1;
  1996. }
  1997. }
  1998. local_actual = (u16)~shift_reg;
  1999. if (local_expected != local_actual)
  2000. status = -EILSEQ;
  2001. }
  2002. if (debug || status) {
  2003. switch (status) {
  2004. case 0:
  2005. printk(KERN_INFO "%s: CRC matched: %04x\n", __func__,
  2006. local_actual);
  2007. break;
  2008. case -EILSEQ:
  2009. printk(KERN_ERR "%s: CRC mismatch: expected %04x, "
  2010. "actual %04x\n", __func__, local_expected,
  2011. local_actual);
  2012. break;
  2013. case -ENODATA:
  2014. printk(KERN_ERR "%s: expected CRC not found, "
  2015. "actual CRC = %04x\n", __func__,
  2016. local_actual);
  2017. break;
  2018. case -EIO:
  2019. printk(KERN_ERR "%s: error: format isn't "
  2020. "recognized.\n", __func__);
  2021. break;
  2022. default:
  2023. printk(KERN_ERR "%s: CRC function returned error "
  2024. "code %d\n", __func__, status);
  2025. break;
  2026. }
  2027. }
  2028. return status;
  2029. }
  2030. static int altera_get_file_info(u8 *p,
  2031. s32 program_size,
  2032. int *format_version,
  2033. int *action_count,
  2034. int *procedure_count)
  2035. {
  2036. int status = -EIO;
  2037. u32 first_word = 0;
  2038. int version = 0;
  2039. if (program_size <= 52L)
  2040. return status;
  2041. first_word = get_unaligned_be32(&p[0]);
  2042. if ((first_word == 0x4A414D00L) || (first_word == 0x4A414D01L)) {
  2043. status = 0;
  2044. version = (first_word & 1L);
  2045. *format_version = version + 1;
  2046. if (version > 0) {
  2047. *action_count = get_unaligned_be32(&p[48]);
  2048. *procedure_count = get_unaligned_be32(&p[52]);
  2049. }
  2050. }
  2051. return status;
  2052. }
  2053. static int altera_get_act_info(u8 *p,
  2054. s32 program_size,
  2055. int index,
  2056. char **name,
  2057. char **description,
  2058. struct altera_procinfo **proc_list)
  2059. {
  2060. int status = -EIO;
  2061. struct altera_procinfo *procptr = NULL;
  2062. struct altera_procinfo *tmpptr = NULL;
  2063. u32 first_word = 0L;
  2064. u32 action_table = 0L;
  2065. u32 proc_table = 0L;
  2066. u32 str_table = 0L;
  2067. u32 note_strings = 0L;
  2068. u32 action_count = 0L;
  2069. u32 proc_count = 0L;
  2070. u32 act_name_id = 0L;
  2071. u32 act_desc_id = 0L;
  2072. u32 act_proc_id = 0L;
  2073. u32 act_proc_name = 0L;
  2074. u8 act_proc_attribute = 0;
  2075. if (program_size <= 52L)
  2076. return status;
  2077. /* Read header information */
  2078. first_word = get_unaligned_be32(&p[0]);
  2079. if (first_word != 0x4A414D01L)
  2080. return status;
  2081. action_table = get_unaligned_be32(&p[4]);
  2082. proc_table = get_unaligned_be32(&p[8]);
  2083. str_table = get_unaligned_be32(&p[12]);
  2084. note_strings = get_unaligned_be32(&p[16]);
  2085. action_count = get_unaligned_be32(&p[48]);
  2086. proc_count = get_unaligned_be32(&p[52]);
  2087. if (index >= action_count)
  2088. return status;
  2089. act_name_id = get_unaligned_be32(&p[action_table + (12 * index)]);
  2090. act_desc_id = get_unaligned_be32(&p[action_table + (12 * index) + 4]);
  2091. act_proc_id = get_unaligned_be32(&p[action_table + (12 * index) + 8]);
  2092. *name = &p[str_table + act_name_id];
  2093. if (act_desc_id < (note_strings - str_table))
  2094. *description = &p[str_table + act_desc_id];
  2095. do {
  2096. act_proc_name = get_unaligned_be32(
  2097. &p[proc_table + (13 * act_proc_id)]);
  2098. act_proc_attribute =
  2099. (p[proc_table + (13 * act_proc_id) + 8] & 0x03);
  2100. procptr = (struct altera_procinfo *)
  2101. kzalloc(sizeof(struct altera_procinfo),
  2102. GFP_KERNEL);
  2103. if (procptr == NULL)
  2104. status = -ENOMEM;
  2105. else {
  2106. procptr->name = &p[str_table + act_proc_name];
  2107. procptr->attrs = act_proc_attribute;
  2108. procptr->next = NULL;
  2109. /* add record to end of linked list */
  2110. if (*proc_list == NULL)
  2111. *proc_list = procptr;
  2112. else {
  2113. tmpptr = *proc_list;
  2114. while (tmpptr->next != NULL)
  2115. tmpptr = tmpptr->next;
  2116. tmpptr->next = procptr;
  2117. }
  2118. }
  2119. act_proc_id = get_unaligned_be32(
  2120. &p[proc_table + (13 * act_proc_id) + 4]);
  2121. } while ((act_proc_id != 0) && (act_proc_id < proc_count));
  2122. return status;
  2123. }
  2124. int altera_init(struct altera_config *config, const struct firmware *fw)
  2125. {
  2126. struct altera_state *astate = NULL;
  2127. struct altera_procinfo *proc_list = NULL;
  2128. struct altera_procinfo *procptr = NULL;
  2129. char *key = NULL;
  2130. char *value = NULL;
  2131. char *action_name = NULL;
  2132. char *description = NULL;
  2133. int exec_result = 0;
  2134. int exit_code = 0;
  2135. int format_version = 0;
  2136. int action_count = 0;
  2137. int procedure_count = 0;
  2138. int index = 0;
  2139. s32 offset = 0L;
  2140. s32 error_address = 0L;
  2141. key = kzalloc(33 * sizeof(char), GFP_KERNEL);
  2142. if (!key)
  2143. return -ENOMEM;
  2144. value = kzalloc(257 * sizeof(char), GFP_KERNEL);
  2145. if (!value)
  2146. return -ENOMEM;
  2147. astate = kzalloc(sizeof(struct altera_state), GFP_KERNEL);
  2148. if (!astate)
  2149. return -ENOMEM;
  2150. astate->config = config;
  2151. if (!astate->config->jtag_io) {
  2152. dprintk(KERN_INFO "%s: using byteblaster!\n", __func__);
  2153. astate->config->jtag_io = netup_jtag_io_lpt;
  2154. }
  2155. altera_check_crc((u8 *)fw->data, fw->size);
  2156. if (debug) {
  2157. altera_get_file_info((u8 *)fw->data, fw->size, &format_version,
  2158. &action_count, &procedure_count);
  2159. printk(KERN_INFO "%s: File format is %s ByteCode format\n",
  2160. __func__, (format_version == 2) ? "Jam STAPL" :
  2161. "pre-standardized Jam 1.1");
  2162. while (altera_get_note((u8 *)fw->data, fw->size,
  2163. &offset, key, value, 256) == 0)
  2164. printk(KERN_INFO "%s: NOTE \"%s\" = \"%s\"\n",
  2165. __func__, key, value);
  2166. }
  2167. if (debug && (format_version == 2) && (action_count > 0)) {
  2168. printk(KERN_INFO "%s: Actions available:\n", __func__);
  2169. for (index = 0; index < action_count; ++index) {
  2170. altera_get_act_info((u8 *)fw->data, fw->size,
  2171. index, &action_name,
  2172. &description,
  2173. &proc_list);
  2174. if (description == NULL)
  2175. printk(KERN_INFO "%s: %s\n",
  2176. __func__,
  2177. action_name);
  2178. else
  2179. printk(KERN_INFO "%s: %s \"%s\"\n",
  2180. __func__,
  2181. action_name,
  2182. description);
  2183. procptr = proc_list;
  2184. while (procptr != NULL) {
  2185. if (procptr->attrs != 0)
  2186. printk(KERN_INFO "%s: %s (%s)\n",
  2187. __func__,
  2188. procptr->name,
  2189. (procptr->attrs == 1) ?
  2190. "optional" : "recommended");
  2191. proc_list = procptr->next;
  2192. kfree(procptr);
  2193. procptr = proc_list;
  2194. }
  2195. }
  2196. printk(KERN_INFO "\n");
  2197. }
  2198. exec_result = altera_execute(astate, (u8 *)fw->data, fw->size,
  2199. &error_address, &exit_code, &format_version);
  2200. if (exit_code)
  2201. exec_result = -EREMOTEIO;
  2202. if ((format_version == 2) && (exec_result == -EINVAL)) {
  2203. if (astate->config->action == NULL)
  2204. printk(KERN_ERR "%s: error: no action specified for "
  2205. "Jam STAPL file.\nprogram terminated.\n",
  2206. __func__);
  2207. else
  2208. printk(KERN_ERR "%s: error: action \"%s\""
  2209. " is not supported "
  2210. "for this Jam STAPL file.\n"
  2211. "Program terminated.\n", __func__,
  2212. astate->config->action);
  2213. } else if (exec_result)
  2214. printk(KERN_ERR "%s: error %d\n", __func__, exec_result);
  2215. kfree(key);
  2216. kfree(value);
  2217. kfree(astate);
  2218. return 0;
  2219. }
  2220. EXPORT_SYMBOL(altera_init);