altera.c 55 KB

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