altera.c 56 KB

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