pmp.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. /* Copyright 2018 SiFive, Inc */
  2. /* SPDX-License-Identifier: Apache-2.0 */
  3. #include <metal/machine.h>
  4. #include <metal/pmp.h>
  5. #include <metal/cpu.h>
  6. #define CONFIG_TO_INT(_config) (*((char *) &(_config)))
  7. #define INT_TO_CONFIG(_int) (*((struct metal_pmp_config *)(char *) &(_int)))
  8. struct metal_pmp *metal_pmp_get_device(void)
  9. {
  10. #ifdef __METAL_DT_PMP_HANDLE
  11. return __METAL_DT_PMP_HANDLE;
  12. #else
  13. return NULL;
  14. #endif
  15. }
  16. /* This function calculates the minimum granularity from the address
  17. * that pmpaddr takes on after writing all ones to pmpaddr when pmpcfg = 0.
  18. *
  19. * Detect the address granularity based on the position of the
  20. * least-significant 1 set in the address.
  21. *
  22. * For example, if the value read from pmpaddr is 0x3ffffc00, the
  23. * least-significant set bit is in bit 10 (counting from 0), resulting
  24. * in a detected granularity of 2^(10 + 2) = 4096.
  25. */
  26. static uintptr_t _get_detected_granularity(uintptr_t address) {
  27. if(address == 0) {
  28. return (uintptr_t) -1;
  29. }
  30. /* Get the index of the least significant set bit */
  31. int index = 0;
  32. while(((address >> index) & 0x1) == 0) {
  33. index += 1;
  34. }
  35. /* The granularity is equal to 2^(index + 2) bytes */
  36. return (1 << (index + 2));
  37. }
  38. /* This function calculates the granularity requested by the user's provided
  39. * value for pmpaddr.
  40. *
  41. * Calculate the requested granularity based on the position of the
  42. * least-significant unset bit.
  43. *
  44. * For example, if the requested address is 0x20009ff, the least-significant
  45. * unset bit is at index 9 (counting from 0), resulting in a requested
  46. * granularity of 2^(9 + 3) = 4096.
  47. */
  48. static uintptr_t _get_pmpaddr_granularity(uintptr_t address) {
  49. /* Get the index of the least significant unset bit */
  50. int index = 0;
  51. while(((address >> index) & 0x1) == 1) {
  52. index += 1;
  53. }
  54. /* The granularity is equal to 2^(index + 3) bytes */
  55. return (1 << (index + 3));
  56. }
  57. /* Get the number of pmp regions for the given hart */
  58. int metal_pmp_num_regions(int hartid)
  59. {
  60. struct metal_cpu *cpu = metal_cpu_get(hartid);
  61. return __metal_driver_cpu_num_pmp_regions(cpu);
  62. }
  63. /* Get the number of pmp regions for the current hart */
  64. static unsigned int _pmp_regions() {
  65. return metal_pmp_num_regions(metal_cpu_get_current_hartid());
  66. }
  67. void metal_pmp_init(struct metal_pmp *pmp) {
  68. if(!pmp) {
  69. return;
  70. }
  71. struct metal_pmp_config init_config = {
  72. .L = METAL_PMP_UNLOCKED,
  73. .A = METAL_PMP_OFF,
  74. .X = 0,
  75. .W = 0,
  76. .R = 0,
  77. };
  78. for(unsigned int i = 0; i < _pmp_regions(); i++) {
  79. metal_pmp_set_region(pmp, i, init_config, 0);
  80. }
  81. /* Detect the region granularity by writing all 1s to pmpaddr0 while
  82. * pmpcfg0 = 0. */
  83. if(metal_pmp_set_address(pmp, 0, -1) != 0) {
  84. /* Failed to detect granularity */
  85. return;
  86. }
  87. /* Calculate the granularity based on the value that pmpaddr0 takes on */
  88. pmp->_granularity[metal_cpu_get_current_hartid()] = _get_detected_granularity(metal_pmp_get_address(pmp, 0));
  89. /* Clear pmpaddr0 */
  90. metal_pmp_set_address(pmp, 0, 0);
  91. }
  92. int metal_pmp_set_region(struct metal_pmp *pmp,
  93. unsigned int region,
  94. struct metal_pmp_config config,
  95. size_t address)
  96. {
  97. struct metal_pmp_config old_config;
  98. size_t old_address;
  99. size_t cfgmask;
  100. size_t pmpcfg;
  101. int rc = 0;
  102. if(!pmp) {
  103. /* Device handle cannot be NULL */
  104. return 1;
  105. }
  106. if(region > _pmp_regions()) {
  107. /* Region outside of supported range */
  108. return 2;
  109. }
  110. if(config.A == METAL_PMP_NA4 && pmp->_granularity[metal_cpu_get_current_hartid()] > 4) {
  111. /* The requested granularity is too small */
  112. return 3;
  113. }
  114. if(config.A == METAL_PMP_NAPOT &&
  115. pmp->_granularity[metal_cpu_get_current_hartid()] > _get_pmpaddr_granularity(address))
  116. {
  117. /* The requested granularity is too small */
  118. return 3;
  119. }
  120. rc = metal_pmp_get_region(pmp, region, &old_config, &old_address);
  121. if(rc) {
  122. /* Error reading region */
  123. return rc;
  124. }
  125. if(old_config.L == METAL_PMP_LOCKED) {
  126. /* Cannot modify locked region */
  127. return 4;
  128. }
  129. /* Update the address first, because if the region is being locked we won't
  130. * be able to modify it after we set the config */
  131. if(old_address != address) {
  132. switch(region) {
  133. case 0:
  134. __asm__("csrw pmpaddr0, %[addr]"
  135. :: [addr] "r" (address) :);
  136. break;
  137. case 1:
  138. __asm__("csrw pmpaddr1, %[addr]"
  139. :: [addr] "r" (address) :);
  140. break;
  141. case 2:
  142. __asm__("csrw pmpaddr2, %[addr]"
  143. :: [addr] "r" (address) :);
  144. break;
  145. case 3:
  146. __asm__("csrw pmpaddr3, %[addr]"
  147. :: [addr] "r" (address) :);
  148. break;
  149. case 4:
  150. __asm__("csrw pmpaddr4, %[addr]"
  151. :: [addr] "r" (address) :);
  152. break;
  153. case 5:
  154. __asm__("csrw pmpaddr5, %[addr]"
  155. :: [addr] "r" (address) :);
  156. break;
  157. case 6:
  158. __asm__("csrw pmpaddr6, %[addr]"
  159. :: [addr] "r" (address) :);
  160. break;
  161. case 7:
  162. __asm__("csrw pmpaddr7, %[addr]"
  163. :: [addr] "r" (address) :);
  164. break;
  165. case 8:
  166. __asm__("csrw pmpaddr8, %[addr]"
  167. :: [addr] "r" (address) :);
  168. break;
  169. case 9:
  170. __asm__("csrw pmpaddr9, %[addr]"
  171. :: [addr] "r" (address) :);
  172. break;
  173. case 10:
  174. __asm__("csrw pmpaddr10, %[addr]"
  175. :: [addr] "r" (address) :);
  176. break;
  177. case 11:
  178. __asm__("csrw pmpaddr11, %[addr]"
  179. :: [addr] "r" (address) :);
  180. break;
  181. case 12:
  182. __asm__("csrw pmpaddr12, %[addr]"
  183. :: [addr] "r" (address) :);
  184. break;
  185. case 13:
  186. __asm__("csrw pmpaddr13, %[addr]"
  187. :: [addr] "r" (address) :);
  188. break;
  189. case 14:
  190. __asm__("csrw pmpaddr14, %[addr]"
  191. :: [addr] "r" (address) :);
  192. break;
  193. case 15:
  194. __asm__("csrw pmpaddr15, %[addr]"
  195. :: [addr] "r" (address) :);
  196. break;
  197. }
  198. }
  199. #if __riscv_xlen==32
  200. if(CONFIG_TO_INT(old_config) != CONFIG_TO_INT(config)) {
  201. /* Mask to clear old pmpcfg */
  202. cfgmask = (0xFF << (8 * (region % 4)) );
  203. pmpcfg = (CONFIG_TO_INT(config) << (8 * (region % 4)) );
  204. switch(region / 4) {
  205. case 0:
  206. __asm__("csrc pmpcfg0, %[mask]"
  207. :: [mask] "r" (cfgmask) :);
  208. __asm__("csrs pmpcfg0, %[cfg]"
  209. :: [cfg] "r" (pmpcfg) :);
  210. break;
  211. case 1:
  212. __asm__("csrc pmpcfg1, %[mask]"
  213. :: [mask] "r" (cfgmask) :);
  214. __asm__("csrs pmpcfg1, %[cfg]"
  215. :: [cfg] "r" (pmpcfg) :);
  216. break;
  217. case 2:
  218. __asm__("csrc pmpcfg2, %[mask]"
  219. :: [mask] "r" (cfgmask) :);
  220. __asm__("csrs pmpcfg2, %[cfg]"
  221. :: [cfg] "r" (pmpcfg) :);
  222. break;
  223. case 3:
  224. __asm__("csrc pmpcfg3, %[mask]"
  225. :: [mask] "r" (cfgmask) :);
  226. __asm__("csrs pmpcfg3, %[cfg]"
  227. :: [cfg] "r" (pmpcfg) :);
  228. break;
  229. }
  230. }
  231. #elif __riscv_xlen==64
  232. if(CONFIG_TO_INT(old_config) != CONFIG_TO_INT(config)) {
  233. /* Mask to clear old pmpcfg */
  234. cfgmask = (0xFF << (8 * (region % 8)) );
  235. pmpcfg = (CONFIG_TO_INT(config) << (8 * (region % 8)) );
  236. switch(region / 8) {
  237. case 0:
  238. __asm__("csrc pmpcfg0, %[mask]"
  239. :: [mask] "r" (cfgmask) :);
  240. __asm__("csrs pmpcfg0, %[cfg]"
  241. :: [cfg] "r" (pmpcfg) :);
  242. break;
  243. case 1:
  244. __asm__("csrc pmpcfg2, %[mask]"
  245. :: [mask] "r" (cfgmask) :);
  246. __asm__("csrs pmpcfg2, %[cfg]"
  247. :: [cfg] "r" (pmpcfg) :);
  248. break;
  249. }
  250. }
  251. #else
  252. #error XLEN is not set to supported value for PMP driver
  253. #endif
  254. return 0;
  255. }
  256. int metal_pmp_get_region(struct metal_pmp *pmp,
  257. unsigned int region,
  258. struct metal_pmp_config *config,
  259. size_t *address)
  260. {
  261. size_t pmpcfg = 0;
  262. char *pmpcfg_convert = (char *)&pmpcfg;
  263. if(!pmp || !config || !address) {
  264. /* NULL pointers are invalid arguments */
  265. return 1;
  266. }
  267. if(region > _pmp_regions()) {
  268. /* Region outside of supported range */
  269. return 2;
  270. }
  271. #if __riscv_xlen==32
  272. switch(region / 4) {
  273. case 0:
  274. __asm__("csrr %[cfg], pmpcfg0"
  275. : [cfg] "=r" (pmpcfg) ::);
  276. break;
  277. case 1:
  278. __asm__("csrr %[cfg], pmpcfg1"
  279. : [cfg] "=r" (pmpcfg) ::);
  280. break;
  281. case 2:
  282. __asm__("csrr %[cfg], pmpcfg2"
  283. : [cfg] "=r" (pmpcfg) ::);
  284. break;
  285. case 3:
  286. __asm__("csrr %[cfg], pmpcfg3"
  287. : [cfg] "=r" (pmpcfg) ::);
  288. break;
  289. }
  290. pmpcfg = (0xFF & (pmpcfg >> (8 * (region % 4)) ) );
  291. #elif __riscv_xlen==64
  292. switch(region / 8) {
  293. case 0:
  294. __asm__("csrr %[cfg], pmpcfg0"
  295. : [cfg] "=r" (pmpcfg) ::);
  296. break;
  297. case 1:
  298. __asm__("csrr %[cfg], pmpcfg2"
  299. : [cfg] "=r" (pmpcfg) ::);
  300. break;
  301. }
  302. pmpcfg = (0xFF & (pmpcfg >> (8 * (region % 8)) ) );
  303. #else
  304. #error XLEN is not set to supported value for PMP driver
  305. #endif
  306. *config = INT_TO_CONFIG(*pmpcfg_convert);
  307. switch(region) {
  308. case 0:
  309. __asm__("csrr %[addr], pmpaddr0"
  310. : [addr] "=r" (*address) ::);
  311. break;
  312. case 1:
  313. __asm__("csrr %[addr], pmpaddr1"
  314. : [addr] "=r" (*address) ::);
  315. break;
  316. case 2:
  317. __asm__("csrr %[addr], pmpaddr2"
  318. : [addr] "=r" (*address) ::);
  319. break;
  320. case 3:
  321. __asm__("csrr %[addr], pmpaddr3"
  322. : [addr] "=r" (*address) ::);
  323. break;
  324. case 4:
  325. __asm__("csrr %[addr], pmpaddr4"
  326. : [addr] "=r" (*address) ::);
  327. break;
  328. case 5:
  329. __asm__("csrr %[addr], pmpaddr5"
  330. : [addr] "=r" (*address) ::);
  331. break;
  332. case 6:
  333. __asm__("csrr %[addr], pmpaddr6"
  334. : [addr] "=r" (*address) ::);
  335. break;
  336. case 7:
  337. __asm__("csrr %[addr], pmpaddr7"
  338. : [addr] "=r" (*address) ::);
  339. break;
  340. case 8:
  341. __asm__("csrr %[addr], pmpaddr8"
  342. : [addr] "=r" (*address) ::);
  343. break;
  344. case 9:
  345. __asm__("csrr %[addr], pmpaddr9"
  346. : [addr] "=r" (*address) ::);
  347. break;
  348. case 10:
  349. __asm__("csrr %[addr], pmpaddr10"
  350. : [addr] "=r" (*address) ::);
  351. break;
  352. case 11:
  353. __asm__("csrr %[addr], pmpaddr11"
  354. : [addr] "=r" (*address) ::);
  355. break;
  356. case 12:
  357. __asm__("csrr %[addr], pmpaddr12"
  358. : [addr] "=r" (*address) ::);
  359. break;
  360. case 13:
  361. __asm__("csrr %[addr], pmpaddr13"
  362. : [addr] "=r" (*address) ::);
  363. break;
  364. case 14:
  365. __asm__("csrr %[addr], pmpaddr14"
  366. : [addr] "=r" (*address) ::);
  367. break;
  368. case 15:
  369. __asm__("csrr %[addr], pmpaddr15"
  370. : [addr] "=r" (*address) ::);
  371. break;
  372. }
  373. return 0;
  374. }
  375. int metal_pmp_lock(struct metal_pmp *pmp, unsigned int region)
  376. {
  377. struct metal_pmp_config config;
  378. size_t address;
  379. int rc = 0;
  380. rc = metal_pmp_get_region(pmp, region, &config, &address);
  381. if(rc) {
  382. return rc;
  383. }
  384. if(config.L == METAL_PMP_LOCKED) {
  385. return 0;
  386. }
  387. config.L = METAL_PMP_LOCKED;
  388. rc = metal_pmp_set_region(pmp, region, config, address);
  389. return rc;
  390. }
  391. int metal_pmp_set_address(struct metal_pmp *pmp, unsigned int region, size_t address)
  392. {
  393. struct metal_pmp_config config;
  394. size_t old_address;
  395. int rc = 0;
  396. rc = metal_pmp_get_region(pmp, region, &config, &old_address);
  397. if(rc) {
  398. return rc;
  399. }
  400. rc = metal_pmp_set_region(pmp, region, config, address);
  401. return rc;
  402. }
  403. size_t metal_pmp_get_address(struct metal_pmp *pmp, unsigned int region)
  404. {
  405. struct metal_pmp_config config;
  406. size_t address = 0;
  407. metal_pmp_get_region(pmp, region, &config, &address);
  408. return address;
  409. }
  410. int metal_pmp_set_address_mode(struct metal_pmp *pmp, unsigned int region, enum metal_pmp_address_mode mode)
  411. {
  412. struct metal_pmp_config config;
  413. size_t address;
  414. int rc = 0;
  415. rc = metal_pmp_get_region(pmp, region, &config, &address);
  416. if(rc) {
  417. return rc;
  418. }
  419. config.A = mode;
  420. rc = metal_pmp_set_region(pmp, region, config, address);
  421. return rc;
  422. }
  423. enum metal_pmp_address_mode metal_pmp_get_address_mode(struct metal_pmp *pmp, unsigned int region)
  424. {
  425. struct metal_pmp_config config;
  426. size_t address = 0;
  427. metal_pmp_get_region(pmp, region, &config, &address);
  428. return config.A;
  429. }
  430. int metal_pmp_set_executable(struct metal_pmp *pmp, unsigned int region, int X)
  431. {
  432. struct metal_pmp_config config;
  433. size_t address;
  434. int rc = 0;
  435. rc = metal_pmp_get_region(pmp, region, &config, &address);
  436. if(rc) {
  437. return rc;
  438. }
  439. config.X = X;
  440. rc = metal_pmp_set_region(pmp, region, config, address);
  441. return rc;
  442. }
  443. int metal_pmp_get_executable(struct metal_pmp *pmp, unsigned int region)
  444. {
  445. struct metal_pmp_config config;
  446. size_t address = 0;
  447. metal_pmp_get_region(pmp, region, &config, &address);
  448. return config.X;
  449. }
  450. int metal_pmp_set_writeable(struct metal_pmp *pmp, unsigned int region, int W)
  451. {
  452. struct metal_pmp_config config;
  453. size_t address;
  454. int rc = 0;
  455. rc = metal_pmp_get_region(pmp, region, &config, &address);
  456. if(rc) {
  457. return rc;
  458. }
  459. config.W = W;
  460. rc = metal_pmp_set_region(pmp, region, config, address);
  461. return rc;
  462. }
  463. int metal_pmp_get_writeable(struct metal_pmp *pmp, unsigned int region)
  464. {
  465. struct metal_pmp_config config;
  466. size_t address = 0;
  467. metal_pmp_get_region(pmp, region, &config, &address);
  468. return config.W;
  469. }
  470. int metal_pmp_set_readable(struct metal_pmp *pmp, unsigned int region, int R)
  471. {
  472. struct metal_pmp_config config;
  473. size_t address;
  474. int rc = 0;
  475. rc = metal_pmp_get_region(pmp, region, &config, &address);
  476. if(rc) {
  477. return rc;
  478. }
  479. config.R = R;
  480. rc = metal_pmp_set_region(pmp, region, config, address);
  481. return rc;
  482. }
  483. int metal_pmp_get_readable(struct metal_pmp *pmp, unsigned int region)
  484. {
  485. struct metal_pmp_config config;
  486. size_t address = 0;
  487. metal_pmp_get_region(pmp, region, &config, &address);
  488. return config.R;
  489. }