isram-driver.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*
  2. * Instruction SRAM accessor functions for the Blackfin
  3. *
  4. * Copyright 2008 Analog Devices Inc.
  5. *
  6. * Licensed under the GPL-2 or later
  7. */
  8. #define pr_fmt(fmt) "isram: " fmt
  9. #include <linux/module.h>
  10. #include <linux/kernel.h>
  11. #include <linux/types.h>
  12. #include <linux/slab.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/sched.h>
  15. #include <asm/blackfin.h>
  16. #include <asm/dma.h>
  17. /*
  18. * IMPORTANT WARNING ABOUT THESE FUNCTIONS
  19. *
  20. * The emulator will not function correctly if a write command is left in
  21. * ITEST_COMMAND or DTEST_COMMAND AND access to cache memory is needed by
  22. * the emulator. To avoid such problems, ensure that both ITEST_COMMAND
  23. * and DTEST_COMMAND are zero when exiting these functions.
  24. */
  25. /*
  26. * On the Blackfin, L1 instruction sram (which operates at core speeds) can not
  27. * be accessed by a normal core load, so we need to go through a few hoops to
  28. * read/write it.
  29. * To try to make it easier - we export a memcpy interface, where either src or
  30. * dest can be in this special L1 memory area.
  31. * The low level read/write functions should not be exposed to the rest of the
  32. * kernel, since they operate on 64-bit data, and need specific address alignment
  33. */
  34. static DEFINE_SPINLOCK(dtest_lock);
  35. /* Takes a void pointer */
  36. #define IADDR2DTEST(x) \
  37. ({ unsigned long __addr = (unsigned long)(x); \
  38. (__addr & 0x47F8) | /* address bits 14 & 10:3 */ \
  39. (__addr & 0x8000) << 23 | /* Bank A/B */ \
  40. (__addr & 0x0800) << 15 | /* address bit 11 */ \
  41. (__addr & 0x3000) << 4 | /* address bits 13:12 */ \
  42. (__addr & 0x8000) << 8 | /* address bit 15 */ \
  43. (0x1000000) | /* instruction access = 1 */ \
  44. (0x4); /* data array = 1 */ \
  45. })
  46. /* Takes a pointer, and returns the offset (in bits) which things should be shifted */
  47. #define ADDR2OFFSET(x) ((((unsigned long)(x)) & 0x7) * 8)
  48. /* Takes a pointer, determines if it is the last byte in the isram 64-bit data type */
  49. #define ADDR2LAST(x) ((((unsigned long)x) & 0x7) == 0x7)
  50. static void isram_write(const void *addr, uint64_t data)
  51. {
  52. uint32_t cmd;
  53. unsigned long flags;
  54. if (unlikely(addr >= (void *)(L1_CODE_START + L1_CODE_LENGTH)))
  55. return;
  56. cmd = IADDR2DTEST(addr) | 2; /* write */
  57. /*
  58. * Writes to DTEST_DATA[0:1] need to be atomic with write to DTEST_COMMAND
  59. * While in exception context - atomicity is guaranteed or double fault
  60. */
  61. spin_lock_irqsave(&dtest_lock, flags);
  62. bfin_write_DTEST_DATA0(data & 0xFFFFFFFF);
  63. bfin_write_DTEST_DATA1(data >> 32);
  64. /* use the builtin, since interrupts are already turned off */
  65. __builtin_bfin_csync();
  66. bfin_write_DTEST_COMMAND(cmd);
  67. __builtin_bfin_csync();
  68. bfin_write_DTEST_COMMAND(0);
  69. __builtin_bfin_csync();
  70. spin_unlock_irqrestore(&dtest_lock, flags);
  71. }
  72. static uint64_t isram_read(const void *addr)
  73. {
  74. uint32_t cmd;
  75. unsigned long flags;
  76. uint64_t ret;
  77. if (unlikely(addr > (void *)(L1_CODE_START + L1_CODE_LENGTH)))
  78. return 0;
  79. cmd = IADDR2DTEST(addr) | 0; /* read */
  80. /*
  81. * Reads of DTEST_DATA[0:1] need to be atomic with write to DTEST_COMMAND
  82. * While in exception context - atomicity is guaranteed or double fault
  83. */
  84. spin_lock_irqsave(&dtest_lock, flags);
  85. /* use the builtin, since interrupts are already turned off */
  86. __builtin_bfin_csync();
  87. bfin_write_DTEST_COMMAND(cmd);
  88. __builtin_bfin_csync();
  89. ret = bfin_read_DTEST_DATA0() | ((uint64_t)bfin_read_DTEST_DATA1() << 32);
  90. bfin_write_DTEST_COMMAND(0);
  91. __builtin_bfin_csync();
  92. spin_unlock_irqrestore(&dtest_lock, flags);
  93. return ret;
  94. }
  95. static bool isram_check_addr(const void *addr, size_t n)
  96. {
  97. if ((addr >= (void *)L1_CODE_START) &&
  98. (addr < (void *)(L1_CODE_START + L1_CODE_LENGTH))) {
  99. if (unlikely((addr + n) > (void *)(L1_CODE_START + L1_CODE_LENGTH))) {
  100. show_stack(NULL, NULL);
  101. pr_err("copy involving %p length (%zu) too long\n", addr, n);
  102. }
  103. return true;
  104. }
  105. return false;
  106. }
  107. /*
  108. * The isram_memcpy() function copies n bytes from memory area src to memory area dest.
  109. * The isram_memcpy() function returns a pointer to dest.
  110. * Either dest or src can be in L1 instruction sram.
  111. */
  112. void *isram_memcpy(void *dest, const void *src, size_t n)
  113. {
  114. uint64_t data_in = 0, data_out = 0;
  115. size_t count;
  116. bool dest_in_l1, src_in_l1, need_data, put_data;
  117. unsigned char byte, *src_byte, *dest_byte;
  118. src_byte = (unsigned char *)src;
  119. dest_byte = (unsigned char *)dest;
  120. dest_in_l1 = isram_check_addr(dest, n);
  121. src_in_l1 = isram_check_addr(src, n);
  122. need_data = true;
  123. put_data = true;
  124. for (count = 0; count < n; count++) {
  125. if (src_in_l1) {
  126. if (need_data) {
  127. data_in = isram_read(src + count);
  128. need_data = false;
  129. }
  130. if (ADDR2LAST(src + count))
  131. need_data = true;
  132. byte = (unsigned char)((data_in >> ADDR2OFFSET(src + count)) & 0xff);
  133. } else {
  134. /* src is in L2 or L3 - so just dereference*/
  135. byte = src_byte[count];
  136. }
  137. if (dest_in_l1) {
  138. if (put_data) {
  139. data_out = isram_read(dest + count);
  140. put_data = false;
  141. }
  142. data_out &= ~((uint64_t)0xff << ADDR2OFFSET(dest + count));
  143. data_out |= ((uint64_t)byte << ADDR2OFFSET(dest + count));
  144. if (ADDR2LAST(dest + count)) {
  145. put_data = true;
  146. isram_write(dest + count, data_out);
  147. }
  148. } else {
  149. /* dest in L2 or L3 - so just dereference */
  150. dest_byte[count] = byte;
  151. }
  152. }
  153. /* make sure we dump the last byte if necessary */
  154. if (dest_in_l1 && !put_data)
  155. isram_write(dest + count, data_out);
  156. return dest;
  157. }
  158. EXPORT_SYMBOL(isram_memcpy);
  159. #ifdef CONFIG_BFIN_ISRAM_SELF_TEST
  160. #define TEST_LEN 0x100
  161. static __init void hex_dump(unsigned char *buf, int len)
  162. {
  163. while (len--)
  164. pr_cont("%02x", *buf++);
  165. }
  166. static __init int isram_read_test(char *sdram, void *l1inst)
  167. {
  168. int i, ret = 0;
  169. uint64_t data1, data2;
  170. pr_info("INFO: running isram_read tests\n");
  171. /* setup some different data to play with */
  172. for (i = 0; i < TEST_LEN; ++i)
  173. sdram[i] = i;
  174. dma_memcpy(l1inst, sdram, TEST_LEN);
  175. /* make sure we can read the L1 inst */
  176. for (i = 0; i < TEST_LEN; i += sizeof(uint64_t)) {
  177. data1 = isram_read(l1inst + i);
  178. memcpy(&data2, sdram + i, sizeof(data2));
  179. if (memcmp(&data1, &data2, sizeof(uint64_t))) {
  180. pr_err("FAIL: isram_read(%p) returned %#llx but wanted %#llx\n",
  181. l1inst + i, data1, data2);
  182. ++ret;
  183. }
  184. }
  185. return ret;
  186. }
  187. static __init int isram_write_test(char *sdram, void *l1inst)
  188. {
  189. int i, ret = 0;
  190. uint64_t data1, data2;
  191. pr_info("INFO: running isram_write tests\n");
  192. /* setup some different data to play with */
  193. memset(sdram, 0, TEST_LEN * 2);
  194. dma_memcpy(l1inst, sdram, TEST_LEN);
  195. for (i = 0; i < TEST_LEN; ++i)
  196. sdram[i] = i;
  197. /* make sure we can write the L1 inst */
  198. for (i = 0; i < TEST_LEN; i += sizeof(uint64_t)) {
  199. memcpy(&data1, sdram + i, sizeof(data1));
  200. isram_write(l1inst + i, data1);
  201. data2 = isram_read(l1inst + i);
  202. if (memcmp(&data1, &data2, sizeof(uint64_t))) {
  203. pr_err("FAIL: isram_write(%p, %#llx) != %#llx\n",
  204. l1inst + i, data1, data2);
  205. ++ret;
  206. }
  207. }
  208. dma_memcpy(sdram + TEST_LEN, l1inst, TEST_LEN);
  209. if (memcmp(sdram, sdram + TEST_LEN, TEST_LEN)) {
  210. pr_err("FAIL: isram_write() did not work properly\n");
  211. ++ret;
  212. }
  213. return ret;
  214. }
  215. static __init int
  216. _isram_memcpy_test(char pattern, void *sdram, void *l1inst, const char *smemcpy,
  217. void *(*fmemcpy)(void *, const void *, size_t))
  218. {
  219. memset(sdram, pattern, TEST_LEN);
  220. fmemcpy(l1inst, sdram, TEST_LEN);
  221. fmemcpy(sdram + TEST_LEN, l1inst, TEST_LEN);
  222. if (memcmp(sdram, sdram + TEST_LEN, TEST_LEN)) {
  223. pr_err("FAIL: %s(%p <=> %p, %#x) failed (data is %#x)\n",
  224. smemcpy, l1inst, sdram, TEST_LEN, pattern);
  225. return 1;
  226. }
  227. return 0;
  228. }
  229. #define _isram_memcpy_test(a, b, c, d) _isram_memcpy_test(a, b, c, #d, d)
  230. static __init int isram_memcpy_test(char *sdram, void *l1inst)
  231. {
  232. int i, j, thisret, ret = 0;
  233. /* check broad isram_memcpy() */
  234. pr_info("INFO: running broad isram_memcpy tests\n");
  235. for (i = 0xf; i >= 0; --i)
  236. ret += _isram_memcpy_test(i, sdram, l1inst, isram_memcpy);
  237. /* check read of small, unaligned, and hardware 64bit limits */
  238. pr_info("INFO: running isram_memcpy (read) tests\n");
  239. for (i = 0; i < TEST_LEN; ++i)
  240. sdram[i] = i;
  241. dma_memcpy(l1inst, sdram, TEST_LEN);
  242. thisret = 0;
  243. for (i = 0; i < TEST_LEN - 32; ++i) {
  244. unsigned char cmp[32];
  245. for (j = 1; j <= 32; ++j) {
  246. memset(cmp, 0, sizeof(cmp));
  247. isram_memcpy(cmp, l1inst + i, j);
  248. if (memcmp(cmp, sdram + i, j)) {
  249. pr_err("FAIL: %p:", l1inst + 1);
  250. hex_dump(cmp, j);
  251. pr_cont(" SDRAM:");
  252. hex_dump(sdram + i, j);
  253. pr_cont("\n");
  254. if (++thisret > 20) {
  255. pr_err("FAIL: skipping remaining series\n");
  256. i = TEST_LEN;
  257. break;
  258. }
  259. }
  260. }
  261. }
  262. ret += thisret;
  263. /* check write of small, unaligned, and hardware 64bit limits */
  264. pr_info("INFO: running isram_memcpy (write) tests\n");
  265. memset(sdram + TEST_LEN, 0, TEST_LEN);
  266. dma_memcpy(l1inst, sdram + TEST_LEN, TEST_LEN);
  267. thisret = 0;
  268. for (i = 0; i < TEST_LEN - 32; ++i) {
  269. unsigned char cmp[32];
  270. for (j = 1; j <= 32; ++j) {
  271. isram_memcpy(l1inst + i, sdram + i, j);
  272. dma_memcpy(cmp, l1inst + i, j);
  273. if (memcmp(cmp, sdram + i, j)) {
  274. pr_err("FAIL: %p:", l1inst + i);
  275. hex_dump(cmp, j);
  276. pr_cont(" SDRAM:");
  277. hex_dump(sdram + i, j);
  278. pr_cont("\n");
  279. if (++thisret > 20) {
  280. pr_err("FAIL: skipping remaining series\n");
  281. i = TEST_LEN;
  282. break;
  283. }
  284. }
  285. }
  286. }
  287. ret += thisret;
  288. return ret;
  289. }
  290. static __init int isram_test_init(void)
  291. {
  292. int ret;
  293. char *sdram;
  294. void *l1inst;
  295. sdram = kmalloc(TEST_LEN * 2, GFP_KERNEL);
  296. if (!sdram) {
  297. pr_warning("SKIP: could not allocate sdram\n");
  298. return 0;
  299. }
  300. l1inst = l1_inst_sram_alloc(TEST_LEN);
  301. if (!l1inst) {
  302. kfree(sdram);
  303. pr_warning("SKIP: could not allocate L1 inst\n");
  304. return 0;
  305. }
  306. /* sanity check initial L1 inst state */
  307. ret = 1;
  308. pr_info("INFO: running initial dma_memcpy checks\n");
  309. if (_isram_memcpy_test(0xa, sdram, l1inst, dma_memcpy))
  310. goto abort;
  311. if (_isram_memcpy_test(0x5, sdram, l1inst, dma_memcpy))
  312. goto abort;
  313. ret = 0;
  314. ret += isram_read_test(sdram, l1inst);
  315. ret += isram_write_test(sdram, l1inst);
  316. ret += isram_memcpy_test(sdram, l1inst);
  317. abort:
  318. sram_free(l1inst);
  319. kfree(sdram);
  320. if (ret)
  321. return -EIO;
  322. pr_info("PASS: all tests worked !\n");
  323. return 0;
  324. }
  325. late_initcall(isram_test_init);
  326. static __exit void isram_test_exit(void)
  327. {
  328. /* stub to allow unloading */
  329. }
  330. module_exit(isram_test_exit);
  331. #endif