isram-driver.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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 & (1 << 11)) << (26 - 11)) | /* addr bit 11 (Way0/Way1) */ \
  39. (1 << 24) | /* instruction access = 1 */ \
  40. ((__addr & (1 << 15)) << (23 - 15)) | /* addr bit 15 (Data Bank) */ \
  41. ((__addr & (3 << 12)) << (16 - 12)) | /* addr bits 13:12 (Subbank) */ \
  42. (__addr & 0x47F8) | /* addr bits 14 & 10:3 */ \
  43. (1 << 2); /* data array = 1 */ \
  44. })
  45. /* Takes a pointer, and returns the offset (in bits) which things should be shifted */
  46. #define ADDR2OFFSET(x) ((((unsigned long)(x)) & 0x7) * 8)
  47. /* Takes a pointer, determines if it is the last byte in the isram 64-bit data type */
  48. #define ADDR2LAST(x) ((((unsigned long)x) & 0x7) == 0x7)
  49. static void isram_write(const void *addr, uint64_t data)
  50. {
  51. uint32_t cmd;
  52. unsigned long flags;
  53. if (unlikely(addr >= (void *)(L1_CODE_START + L1_CODE_LENGTH)))
  54. return;
  55. cmd = IADDR2DTEST(addr) | 2; /* write */
  56. /*
  57. * Writes to DTEST_DATA[0:1] need to be atomic with write to DTEST_COMMAND
  58. * While in exception context - atomicity is guaranteed or double fault
  59. */
  60. spin_lock_irqsave(&dtest_lock, flags);
  61. bfin_write_DTEST_DATA0(data & 0xFFFFFFFF);
  62. bfin_write_DTEST_DATA1(data >> 32);
  63. /* use the builtin, since interrupts are already turned off */
  64. __builtin_bfin_csync();
  65. bfin_write_DTEST_COMMAND(cmd);
  66. __builtin_bfin_csync();
  67. bfin_write_DTEST_COMMAND(0);
  68. __builtin_bfin_csync();
  69. spin_unlock_irqrestore(&dtest_lock, flags);
  70. }
  71. static uint64_t isram_read(const void *addr)
  72. {
  73. uint32_t cmd;
  74. unsigned long flags;
  75. uint64_t ret;
  76. if (unlikely(addr > (void *)(L1_CODE_START + L1_CODE_LENGTH)))
  77. return 0;
  78. cmd = IADDR2DTEST(addr) | 0; /* read */
  79. /*
  80. * Reads of DTEST_DATA[0:1] need to be atomic with write to DTEST_COMMAND
  81. * While in exception context - atomicity is guaranteed or double fault
  82. */
  83. spin_lock_irqsave(&dtest_lock, flags);
  84. /* use the builtin, since interrupts are already turned off */
  85. __builtin_bfin_csync();
  86. bfin_write_DTEST_COMMAND(cmd);
  87. __builtin_bfin_csync();
  88. ret = bfin_read_DTEST_DATA0() | ((uint64_t)bfin_read_DTEST_DATA1() << 32);
  89. bfin_write_DTEST_COMMAND(0);
  90. __builtin_bfin_csync();
  91. spin_unlock_irqrestore(&dtest_lock, flags);
  92. return ret;
  93. }
  94. static bool isram_check_addr(const void *addr, size_t n)
  95. {
  96. if ((addr >= (void *)L1_CODE_START) &&
  97. (addr < (void *)(L1_CODE_START + L1_CODE_LENGTH))) {
  98. if (unlikely((addr + n) > (void *)(L1_CODE_START + L1_CODE_LENGTH))) {
  99. show_stack(NULL, NULL);
  100. pr_err("copy involving %p length (%zu) too long\n", addr, n);
  101. }
  102. return true;
  103. }
  104. return false;
  105. }
  106. /*
  107. * The isram_memcpy() function copies n bytes from memory area src to memory area dest.
  108. * The isram_memcpy() function returns a pointer to dest.
  109. * Either dest or src can be in L1 instruction sram.
  110. */
  111. void *isram_memcpy(void *dest, const void *src, size_t n)
  112. {
  113. uint64_t data_in = 0, data_out = 0;
  114. size_t count;
  115. bool dest_in_l1, src_in_l1, need_data, put_data;
  116. unsigned char byte, *src_byte, *dest_byte;
  117. src_byte = (unsigned char *)src;
  118. dest_byte = (unsigned char *)dest;
  119. dest_in_l1 = isram_check_addr(dest, n);
  120. src_in_l1 = isram_check_addr(src, n);
  121. need_data = true;
  122. put_data = true;
  123. for (count = 0; count < n; count++) {
  124. if (src_in_l1) {
  125. if (need_data) {
  126. data_in = isram_read(src + count);
  127. need_data = false;
  128. }
  129. if (ADDR2LAST(src + count))
  130. need_data = true;
  131. byte = (unsigned char)((data_in >> ADDR2OFFSET(src + count)) & 0xff);
  132. } else {
  133. /* src is in L2 or L3 - so just dereference*/
  134. byte = src_byte[count];
  135. }
  136. if (dest_in_l1) {
  137. if (put_data) {
  138. data_out = isram_read(dest + count);
  139. put_data = false;
  140. }
  141. data_out &= ~((uint64_t)0xff << ADDR2OFFSET(dest + count));
  142. data_out |= ((uint64_t)byte << ADDR2OFFSET(dest + count));
  143. if (ADDR2LAST(dest + count)) {
  144. put_data = true;
  145. isram_write(dest + count, data_out);
  146. }
  147. } else {
  148. /* dest in L2 or L3 - so just dereference */
  149. dest_byte[count] = byte;
  150. }
  151. }
  152. /* make sure we dump the last byte if necessary */
  153. if (dest_in_l1 && !put_data)
  154. isram_write(dest + count, data_out);
  155. return dest;
  156. }
  157. EXPORT_SYMBOL(isram_memcpy);
  158. #ifdef CONFIG_BFIN_ISRAM_SELF_TEST
  159. static int test_len = 0x20000;
  160. static __init void hex_dump(unsigned char *buf, int len)
  161. {
  162. while (len--)
  163. pr_cont("%02x", *buf++);
  164. }
  165. static __init int isram_read_test(char *sdram, void *l1inst)
  166. {
  167. int i, ret = 0;
  168. uint64_t data1, data2;
  169. pr_info("INFO: running isram_read tests\n");
  170. /* setup some different data to play with */
  171. for (i = 0; i < test_len; ++i)
  172. sdram[i] = i % 255;
  173. dma_memcpy(l1inst, sdram, test_len);
  174. /* make sure we can read the L1 inst */
  175. for (i = 0; i < test_len; i += sizeof(uint64_t)) {
  176. data1 = isram_read(l1inst + i);
  177. memcpy(&data2, sdram + i, sizeof(data2));
  178. if (data1 != data2) {
  179. pr_err("FAIL: isram_read(%p) returned %#llx but wanted %#llx\n",
  180. l1inst + i, data1, data2);
  181. ++ret;
  182. }
  183. }
  184. return ret;
  185. }
  186. static __init int isram_write_test(char *sdram, void *l1inst)
  187. {
  188. int i, ret = 0;
  189. uint64_t data1, data2;
  190. pr_info("INFO: running isram_write tests\n");
  191. /* setup some different data to play with */
  192. memset(sdram, 0, test_len * 2);
  193. dma_memcpy(l1inst, sdram, test_len);
  194. for (i = 0; i < test_len; ++i)
  195. sdram[i] = i % 255;
  196. /* make sure we can write the L1 inst */
  197. for (i = 0; i < test_len; i += sizeof(uint64_t)) {
  198. memcpy(&data1, sdram + i, sizeof(data1));
  199. isram_write(l1inst + i, data1);
  200. data2 = isram_read(l1inst + i);
  201. if (data1 != data2) {
  202. pr_err("FAIL: isram_write(%p, %#llx) != %#llx\n",
  203. l1inst + i, data1, data2);
  204. ++ret;
  205. }
  206. }
  207. dma_memcpy(sdram + test_len, l1inst, test_len);
  208. if (memcmp(sdram, sdram + test_len, test_len)) {
  209. pr_err("FAIL: isram_write() did not work properly\n");
  210. ++ret;
  211. }
  212. return ret;
  213. }
  214. static __init int
  215. _isram_memcpy_test(char pattern, void *sdram, void *l1inst, const char *smemcpy,
  216. void *(*fmemcpy)(void *, const void *, size_t))
  217. {
  218. memset(sdram, pattern, test_len);
  219. fmemcpy(l1inst, sdram, test_len);
  220. fmemcpy(sdram + test_len, l1inst, test_len);
  221. if (memcmp(sdram, sdram + test_len, test_len)) {
  222. pr_err("FAIL: %s(%p <=> %p, %#x) failed (data is %#x)\n",
  223. smemcpy, l1inst, sdram, test_len, pattern);
  224. return 1;
  225. }
  226. return 0;
  227. }
  228. #define _isram_memcpy_test(a, b, c, d) _isram_memcpy_test(a, b, c, #d, d)
  229. static __init int isram_memcpy_test(char *sdram, void *l1inst)
  230. {
  231. int i, j, thisret, ret = 0;
  232. /* check broad isram_memcpy() */
  233. pr_info("INFO: running broad isram_memcpy tests\n");
  234. for (i = 0xf; i >= 0; --i)
  235. ret += _isram_memcpy_test(i, sdram, l1inst, isram_memcpy);
  236. /* check read of small, unaligned, and hardware 64bit limits */
  237. pr_info("INFO: running isram_memcpy (read) tests\n");
  238. /* setup some different data to play with */
  239. for (i = 0; i < test_len; ++i)
  240. sdram[i] = i % 255;
  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. /* Try to test as much of L1SRAM as possible */
  296. while (test_len) {
  297. test_len >>= 1;
  298. l1inst = l1_inst_sram_alloc(test_len);
  299. if (l1inst)
  300. break;
  301. }
  302. if (!l1inst) {
  303. pr_warning("SKIP: could not allocate L1 inst\n");
  304. return 0;
  305. }
  306. pr_info("INFO: testing %#x bytes (%p - %p)\n",
  307. test_len, l1inst, l1inst + test_len);
  308. sdram = kmalloc(test_len * 2, GFP_KERNEL);
  309. if (!sdram) {
  310. sram_free(l1inst);
  311. pr_warning("SKIP: could not allocate sdram\n");
  312. return 0;
  313. }
  314. /* sanity check initial L1 inst state */
  315. ret = 1;
  316. pr_info("INFO: running initial dma_memcpy checks %p\n", sdram);
  317. if (_isram_memcpy_test(0xa, sdram, l1inst, dma_memcpy))
  318. goto abort;
  319. if (_isram_memcpy_test(0x5, sdram, l1inst, dma_memcpy))
  320. goto abort;
  321. ret = 0;
  322. ret += isram_read_test(sdram, l1inst);
  323. ret += isram_write_test(sdram, l1inst);
  324. ret += isram_memcpy_test(sdram, l1inst);
  325. abort:
  326. sram_free(l1inst);
  327. kfree(sdram);
  328. if (ret)
  329. return -EIO;
  330. pr_info("PASS: all tests worked !\n");
  331. return 0;
  332. }
  333. late_initcall(isram_test_init);
  334. static __exit void isram_test_exit(void)
  335. {
  336. /* stub to allow unloading */
  337. }
  338. module_exit(isram_test_exit);
  339. #endif