isram-driver.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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 <linux/sched/debug.h>
  16. #include <asm/blackfin.h>
  17. #include <asm/dma.h>
  18. /*
  19. * IMPORTANT WARNING ABOUT THESE FUNCTIONS
  20. *
  21. * The emulator will not function correctly if a write command is left in
  22. * ITEST_COMMAND or DTEST_COMMAND AND access to cache memory is needed by
  23. * the emulator. To avoid such problems, ensure that both ITEST_COMMAND
  24. * and DTEST_COMMAND are zero when exiting these functions.
  25. */
  26. /*
  27. * On the Blackfin, L1 instruction sram (which operates at core speeds) can not
  28. * be accessed by a normal core load, so we need to go through a few hoops to
  29. * read/write it.
  30. * To try to make it easier - we export a memcpy interface, where either src or
  31. * dest can be in this special L1 memory area.
  32. * The low level read/write functions should not be exposed to the rest of the
  33. * kernel, since they operate on 64-bit data, and need specific address alignment
  34. */
  35. static DEFINE_SPINLOCK(dtest_lock);
  36. /* Takes a void pointer */
  37. #define IADDR2DTEST(x) \
  38. ({ unsigned long __addr = (unsigned long)(x); \
  39. ((__addr & (1 << 11)) << (26 - 11)) | /* addr bit 11 (Way0/Way1) */ \
  40. (1 << 24) | /* instruction access = 1 */ \
  41. ((__addr & (1 << 15)) << (23 - 15)) | /* addr bit 15 (Data Bank) */ \
  42. ((__addr & (3 << 12)) << (16 - 12)) | /* addr bits 13:12 (Subbank) */ \
  43. (__addr & 0x47F8) | /* addr bits 14 & 10:3 */ \
  44. (1 << 2); /* 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. static int test_len = 0x20000;
  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 % 255;
  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 (data1 != data2) {
  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 % 255;
  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 (data1 != data2) {
  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. /* setup some different data to play with */
  240. for (i = 0; i < test_len; ++i)
  241. sdram[i] = i % 255;
  242. dma_memcpy(l1inst, sdram, test_len);
  243. thisret = 0;
  244. for (i = 0; i < test_len - 32; ++i) {
  245. unsigned char cmp[32];
  246. for (j = 1; j <= 32; ++j) {
  247. memset(cmp, 0, sizeof(cmp));
  248. isram_memcpy(cmp, l1inst + i, j);
  249. if (memcmp(cmp, sdram + i, j)) {
  250. pr_err("FAIL: %p:", l1inst + 1);
  251. hex_dump(cmp, j);
  252. pr_cont(" SDRAM:");
  253. hex_dump(sdram + i, j);
  254. pr_cont("\n");
  255. if (++thisret > 20) {
  256. pr_err("FAIL: skipping remaining series\n");
  257. i = test_len;
  258. break;
  259. }
  260. }
  261. }
  262. }
  263. ret += thisret;
  264. /* check write of small, unaligned, and hardware 64bit limits */
  265. pr_info("INFO: running isram_memcpy (write) tests\n");
  266. memset(sdram + test_len, 0, test_len);
  267. dma_memcpy(l1inst, sdram + test_len, test_len);
  268. thisret = 0;
  269. for (i = 0; i < test_len - 32; ++i) {
  270. unsigned char cmp[32];
  271. for (j = 1; j <= 32; ++j) {
  272. isram_memcpy(l1inst + i, sdram + i, j);
  273. dma_memcpy(cmp, l1inst + i, j);
  274. if (memcmp(cmp, sdram + i, j)) {
  275. pr_err("FAIL: %p:", l1inst + i);
  276. hex_dump(cmp, j);
  277. pr_cont(" SDRAM:");
  278. hex_dump(sdram + i, j);
  279. pr_cont("\n");
  280. if (++thisret > 20) {
  281. pr_err("FAIL: skipping remaining series\n");
  282. i = test_len;
  283. break;
  284. }
  285. }
  286. }
  287. }
  288. ret += thisret;
  289. return ret;
  290. }
  291. static __init int isram_test_init(void)
  292. {
  293. int ret;
  294. char *sdram;
  295. void *l1inst;
  296. /* Try to test as much of L1SRAM as possible */
  297. while (test_len) {
  298. test_len >>= 1;
  299. l1inst = l1_inst_sram_alloc(test_len);
  300. if (l1inst)
  301. break;
  302. }
  303. if (!l1inst) {
  304. pr_warning("SKIP: could not allocate L1 inst\n");
  305. return 0;
  306. }
  307. pr_info("INFO: testing %#x bytes (%p - %p)\n",
  308. test_len, l1inst, l1inst + test_len);
  309. sdram = kmalloc(test_len * 2, GFP_KERNEL);
  310. if (!sdram) {
  311. sram_free(l1inst);
  312. pr_warning("SKIP: could not allocate sdram\n");
  313. return 0;
  314. }
  315. /* sanity check initial L1 inst state */
  316. ret = 1;
  317. pr_info("INFO: running initial dma_memcpy checks %p\n", sdram);
  318. if (_isram_memcpy_test(0xa, sdram, l1inst, dma_memcpy))
  319. goto abort;
  320. if (_isram_memcpy_test(0x5, sdram, l1inst, dma_memcpy))
  321. goto abort;
  322. ret = 0;
  323. ret += isram_read_test(sdram, l1inst);
  324. ret += isram_write_test(sdram, l1inst);
  325. ret += isram_memcpy_test(sdram, l1inst);
  326. abort:
  327. sram_free(l1inst);
  328. kfree(sdram);
  329. if (ret)
  330. return -EIO;
  331. pr_info("PASS: all tests worked !\n");
  332. return 0;
  333. }
  334. late_initcall(isram_test_init);
  335. static __exit void isram_test_exit(void)
  336. {
  337. /* stub to allow unloading */
  338. }
  339. module_exit(isram_test_exit);
  340. #endif