sbi_platform.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /*
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 2019 Western Digital Corporation or its affiliates.
  5. *
  6. * Authors:
  7. * Anup Patel <anup.patel@wdc.com>
  8. */
  9. #ifndef __SBI_PLATFORM_H__
  10. #define __SBI_PLATFORM_H__
  11. /**
  12. * OpenSBI 32-bit platform version with:
  13. * 1. upper 16-bits as major number
  14. * 2. lower 16-bits as minor number
  15. */
  16. #define SBI_PLATFORM_VERSION(Major, Minor) ((Major << 16) | Minor)
  17. /** Offset of opensbi_version in struct sbi_platform */
  18. #define SBI_PLATFORM_OPENSBI_VERSION_OFFSET (0x00)
  19. /** Offset of platform_version in struct sbi_platform */
  20. #define SBI_PLATFORM_VERSION_OFFSET (0x04)
  21. /** Offset of name in struct sbi_platform */
  22. #define SBI_PLATFORM_NAME_OFFSET (0x08)
  23. /** Offset of features in struct sbi_platform */
  24. #define SBI_PLATFORM_FEATURES_OFFSET (0x48)
  25. /** Offset of hart_count in struct sbi_platform */
  26. #define SBI_PLATFORM_HART_COUNT_OFFSET (0x50)
  27. /** Offset of hart_stack_size in struct sbi_platform */
  28. #define SBI_PLATFORM_HART_STACK_SIZE_OFFSET (0x54)
  29. /** Offset of platform_ops_addr in struct sbi_platform */
  30. #define SBI_PLATFORM_OPS_OFFSET (0x58)
  31. /** Offset of firmware_context in struct sbi_platform */
  32. #define SBI_PLATFORM_FIRMWARE_CONTEXT_OFFSET (0x58 + __SIZEOF_POINTER__)
  33. /** Offset of hart_index2id in struct sbi_platform */
  34. #define SBI_PLATFORM_HART_INDEX2ID_OFFSET (0x58 + (__SIZEOF_POINTER__ * 2))
  35. #define SBI_PLATFORM_TLB_RANGE_FLUSH_LIMIT_DEFAULT (1UL << 12)
  36. #ifndef __ASSEMBLER__
  37. #include <sbi/sbi_ecall_interface.h>
  38. #include <sbi/sbi_error.h>
  39. #include <sbi/sbi_scratch.h>
  40. #include <sbi/sbi_version.h>
  41. struct sbi_domain_memregion;
  42. struct sbi_trap_info;
  43. struct sbi_trap_regs;
  44. /** Possible feature flags of a platform */
  45. enum sbi_platform_features {
  46. /** Platform has fault delegation support */
  47. SBI_PLATFORM_HAS_MFAULTS_DELEGATION = (1 << 1),
  48. /** Last index of Platform features*/
  49. SBI_PLATFORM_HAS_LAST_FEATURE = SBI_PLATFORM_HAS_MFAULTS_DELEGATION,
  50. };
  51. /** Default feature set for a platform */
  52. #define SBI_PLATFORM_DEFAULT_FEATURES \
  53. (SBI_PLATFORM_HAS_MFAULTS_DELEGATION)
  54. /** Platform functions */
  55. struct sbi_platform_operations {
  56. /** Platform early initialization */
  57. int (*early_init)(bool cold_boot);
  58. /** Platform final initialization */
  59. int (*final_init)(bool cold_boot);
  60. /** Platform early exit */
  61. void (*early_exit)(void);
  62. /** Platform final exit */
  63. void (*final_exit)(void);
  64. /**
  65. * For platforms that do not implement misa, non-standard
  66. * methods are needed to determine cpu extension.
  67. */
  68. int (*misa_check_extension)(char ext);
  69. /**
  70. * For platforms that do not implement misa, non-standard
  71. * methods are needed to get MXL field of misa.
  72. */
  73. int (*misa_get_xlen)(void);
  74. /** Initialize (or populate) domains for the platform */
  75. int (*domains_init)(void);
  76. /** Initialize the platform console */
  77. int (*console_init)(void);
  78. /** Initialize the platform interrupt controller for current HART */
  79. int (*irqchip_init)(bool cold_boot);
  80. /** Exit the platform interrupt controller for current HART */
  81. void (*irqchip_exit)(void);
  82. /** Initialize IPI for current HART */
  83. int (*ipi_init)(bool cold_boot);
  84. /** Exit IPI for current HART */
  85. void (*ipi_exit)(void);
  86. /** Get tlb flush limit value **/
  87. u64 (*get_tlbr_flush_limit)(void);
  88. /** Initialize platform timer for current HART */
  89. int (*timer_init)(bool cold_boot);
  90. /** Exit platform timer for current HART */
  91. void (*timer_exit)(void);
  92. /** platform specific SBI extension implementation probe function */
  93. int (*vendor_ext_check)(long extid);
  94. /** platform specific SBI extension implementation provider */
  95. int (*vendor_ext_provider)(long extid, long funcid,
  96. const struct sbi_trap_regs *regs,
  97. unsigned long *out_value,
  98. struct sbi_trap_info *out_trap);
  99. };
  100. /** Platform default per-HART stack size for exception/interrupt handling */
  101. #define SBI_PLATFORM_DEFAULT_HART_STACK_SIZE 8192
  102. /** Representation of a platform */
  103. struct sbi_platform {
  104. /**
  105. * OpenSBI version this sbi_platform is based on.
  106. * It's a 32-bit value where upper 16-bits are major number
  107. * and lower 16-bits are minor number
  108. */
  109. u32 opensbi_version;
  110. /**
  111. * OpenSBI platform version released by vendor.
  112. * It's a 32-bit value where upper 16-bits are major number
  113. * and lower 16-bits are minor number
  114. */
  115. u32 platform_version;
  116. /** Name of the platform */
  117. char name[64];
  118. /** Supported features */
  119. u64 features;
  120. /** Total number of HARTs */
  121. u32 hart_count;
  122. /** Per-HART stack size for exception/interrupt handling */
  123. u32 hart_stack_size;
  124. /** Pointer to sbi platform operations */
  125. unsigned long platform_ops_addr;
  126. /** Pointer to system firmware specific context */
  127. unsigned long firmware_context;
  128. /**
  129. * HART index to HART id table
  130. *
  131. * For used HART index <abc>:
  132. * hart_index2id[<abc>] = some HART id
  133. * For unused HART index <abc>:
  134. * hart_index2id[<abc>] = -1U
  135. *
  136. * If hart_index2id == NULL then we assume identity mapping
  137. * hart_index2id[<abc>] = <abc>
  138. *
  139. * We have only two restrictions:
  140. * 1. HART index < sbi_platform hart_count
  141. * 2. HART id < SBI_HARTMASK_MAX_BITS
  142. */
  143. const u32 *hart_index2id;
  144. };
  145. /** Get pointer to sbi_platform for sbi_scratch pointer */
  146. #define sbi_platform_ptr(__s) \
  147. ((const struct sbi_platform *)((__s)->platform_addr))
  148. /** Get pointer to sbi_platform for current HART */
  149. #define sbi_platform_thishart_ptr() ((const struct sbi_platform *) \
  150. (sbi_scratch_thishart_ptr()->platform_addr))
  151. /** Get pointer to platform_ops_addr from platform pointer **/
  152. #define sbi_platform_ops(__p) \
  153. ((const struct sbi_platform_operations *)(__p)->platform_ops_addr)
  154. /** Check whether the platform supports fault delegation */
  155. #define sbi_platform_has_mfaults_delegation(__p) \
  156. ((__p)->features & SBI_PLATFORM_HAS_MFAULTS_DELEGATION)
  157. /**
  158. * Get HART index for the given HART
  159. *
  160. * @param plat pointer to struct sbi_platform
  161. * @param hartid HART ID
  162. *
  163. * @return 0 <= value < hart_count for valid HART otherwise -1U
  164. */
  165. u32 sbi_platform_hart_index(const struct sbi_platform *plat, u32 hartid);
  166. /**
  167. * Get the platform features in string format
  168. *
  169. * @param plat pointer to struct sbi_platform
  170. * @param features_str pointer to a char array where the features string will be
  171. * updated
  172. * @param nfstr length of the features_str. The feature string will be truncated
  173. * if nfstr is not long enough.
  174. */
  175. void sbi_platform_get_features_str(const struct sbi_platform *plat,
  176. char *features_str, int nfstr);
  177. /**
  178. * Get name of the platform
  179. *
  180. * @param plat pointer to struct sbi_platform
  181. *
  182. * @return pointer to platform name on success and "Unknown" on failure
  183. */
  184. static inline const char *sbi_platform_name(const struct sbi_platform *plat)
  185. {
  186. if (plat)
  187. return plat->name;
  188. return "Unknown";
  189. }
  190. /**
  191. * Get the platform features
  192. *
  193. * @param plat pointer to struct sbi_platform
  194. *
  195. * @return the features value currently set for the given platform
  196. */
  197. static inline unsigned long sbi_platform_get_features(
  198. const struct sbi_platform *plat)
  199. {
  200. if (plat)
  201. return plat->features;
  202. return 0;
  203. }
  204. /**
  205. * Get platform specific tlb range flush maximum value. Any request with size
  206. * higher than this is upgraded to a full flush.
  207. *
  208. * @param plat pointer to struct sbi_platform
  209. *
  210. * @return tlb range flush limit value. Returns a default (page size) if not
  211. * defined by platform.
  212. */
  213. static inline u64 sbi_platform_tlbr_flush_limit(const struct sbi_platform *plat)
  214. {
  215. if (plat && sbi_platform_ops(plat)->get_tlbr_flush_limit)
  216. return sbi_platform_ops(plat)->get_tlbr_flush_limit();
  217. return SBI_PLATFORM_TLB_RANGE_FLUSH_LIMIT_DEFAULT;
  218. }
  219. /**
  220. * Get total number of HARTs supported by the platform
  221. *
  222. * @param plat pointer to struct sbi_platform
  223. *
  224. * @return total number of HARTs
  225. */
  226. static inline u32 sbi_platform_hart_count(const struct sbi_platform *plat)
  227. {
  228. if (plat)
  229. return plat->hart_count;
  230. return 0;
  231. }
  232. /**
  233. * Get per-HART stack size for exception/interrupt handling
  234. *
  235. * @param plat pointer to struct sbi_platform
  236. *
  237. * @return stack size in bytes
  238. */
  239. static inline u32 sbi_platform_hart_stack_size(const struct sbi_platform *plat)
  240. {
  241. if (plat)
  242. return plat->hart_stack_size;
  243. return 0;
  244. }
  245. /**
  246. * Check whether given HART is invalid
  247. *
  248. * @param plat pointer to struct sbi_platform
  249. * @param hartid HART ID
  250. *
  251. * @return TRUE if HART is invalid and FALSE otherwise
  252. */
  253. static inline bool sbi_platform_hart_invalid(const struct sbi_platform *plat,
  254. u32 hartid)
  255. {
  256. if (!plat)
  257. return TRUE;
  258. if (plat->hart_count <= sbi_platform_hart_index(plat, hartid))
  259. return TRUE;
  260. return FALSE;
  261. }
  262. /**
  263. * Early initialization for current HART
  264. *
  265. * @param plat pointer to struct sbi_platform
  266. * @param cold_boot whether cold boot (TRUE) or warm_boot (FALSE)
  267. *
  268. * @return 0 on success and negative error code on failure
  269. */
  270. static inline int sbi_platform_early_init(const struct sbi_platform *plat,
  271. bool cold_boot)
  272. {
  273. if (plat && sbi_platform_ops(plat)->early_init)
  274. return sbi_platform_ops(plat)->early_init(cold_boot);
  275. return 0;
  276. }
  277. /**
  278. * Final initialization for current HART
  279. *
  280. * @param plat pointer to struct sbi_platform
  281. * @param cold_boot whether cold boot (TRUE) or warm_boot (FALSE)
  282. *
  283. * @return 0 on success and negative error code on failure
  284. */
  285. static inline int sbi_platform_final_init(const struct sbi_platform *plat,
  286. bool cold_boot)
  287. {
  288. if (plat && sbi_platform_ops(plat)->final_init)
  289. return sbi_platform_ops(plat)->final_init(cold_boot);
  290. return 0;
  291. }
  292. /**
  293. * Early exit for current HART
  294. *
  295. * @param plat pointer to struct sbi_platform
  296. */
  297. static inline void sbi_platform_early_exit(const struct sbi_platform *plat)
  298. {
  299. if (plat && sbi_platform_ops(plat)->early_exit)
  300. sbi_platform_ops(plat)->early_exit();
  301. }
  302. /**
  303. * Final exit for current HART
  304. *
  305. * @param plat pointer to struct sbi_platform
  306. */
  307. static inline void sbi_platform_final_exit(const struct sbi_platform *plat)
  308. {
  309. if (plat && sbi_platform_ops(plat)->final_exit)
  310. sbi_platform_ops(plat)->final_exit();
  311. }
  312. /**
  313. * Check CPU extension in MISA
  314. *
  315. * @param plat pointer to struct sbi_platform
  316. * @param ext shorthand letter for CPU extensions
  317. *
  318. * @return zero for not-supported and non-zero for supported
  319. */
  320. static inline int sbi_platform_misa_extension(const struct sbi_platform *plat,
  321. char ext)
  322. {
  323. if (plat && sbi_platform_ops(plat)->misa_check_extension)
  324. return sbi_platform_ops(plat)->misa_check_extension(ext);
  325. return 0;
  326. }
  327. /**
  328. * Get MXL field of MISA
  329. *
  330. * @param plat pointer to struct sbi_platform
  331. *
  332. * @return 1/2/3 on success and error code on failure
  333. */
  334. static inline int sbi_platform_misa_xlen(const struct sbi_platform *plat)
  335. {
  336. if (plat && sbi_platform_ops(plat)->misa_get_xlen)
  337. return sbi_platform_ops(plat)->misa_get_xlen();
  338. return -1;
  339. }
  340. /**
  341. * Initialize (or populate) domains for the platform
  342. *
  343. * @param plat pointer to struct sbi_platform
  344. *
  345. * @return 0 on success and negative error code on failure
  346. */
  347. static inline int sbi_platform_domains_init(const struct sbi_platform *plat)
  348. {
  349. if (plat && sbi_platform_ops(plat)->domains_init)
  350. return sbi_platform_ops(plat)->domains_init();
  351. return 0;
  352. }
  353. /**
  354. * Initialize the platform console
  355. *
  356. * @param plat pointer to struct sbi_platform
  357. *
  358. * @return 0 on success and negative error code on failure
  359. */
  360. static inline int sbi_platform_console_init(const struct sbi_platform *plat)
  361. {
  362. if (plat && sbi_platform_ops(plat)->console_init)
  363. return sbi_platform_ops(plat)->console_init();
  364. return 0;
  365. }
  366. /**
  367. * Initialize the platform interrupt controller for current HART
  368. *
  369. * @param plat pointer to struct sbi_platform
  370. * @param cold_boot whether cold boot (TRUE) or warm_boot (FALSE)
  371. *
  372. * @return 0 on success and negative error code on failure
  373. */
  374. static inline int sbi_platform_irqchip_init(const struct sbi_platform *plat,
  375. bool cold_boot)
  376. {
  377. if (plat && sbi_platform_ops(plat)->irqchip_init)
  378. return sbi_platform_ops(plat)->irqchip_init(cold_boot);
  379. return 0;
  380. }
  381. /**
  382. * Exit the platform interrupt controller for current HART
  383. *
  384. * @param plat pointer to struct sbi_platform
  385. */
  386. static inline void sbi_platform_irqchip_exit(const struct sbi_platform *plat)
  387. {
  388. if (plat && sbi_platform_ops(plat)->irqchip_exit)
  389. sbi_platform_ops(plat)->irqchip_exit();
  390. }
  391. /**
  392. * Initialize the platform IPI support for current HART
  393. *
  394. * @param plat pointer to struct sbi_platform
  395. * @param cold_boot whether cold boot (TRUE) or warm_boot (FALSE)
  396. *
  397. * @return 0 on success and negative error code on failure
  398. */
  399. static inline int sbi_platform_ipi_init(const struct sbi_platform *plat,
  400. bool cold_boot)
  401. {
  402. if (plat && sbi_platform_ops(plat)->ipi_init)
  403. return sbi_platform_ops(plat)->ipi_init(cold_boot);
  404. return 0;
  405. }
  406. /**
  407. * Exit the platform IPI support for current HART
  408. *
  409. * @param plat pointer to struct sbi_platform
  410. */
  411. static inline void sbi_platform_ipi_exit(const struct sbi_platform *plat)
  412. {
  413. if (plat && sbi_platform_ops(plat)->ipi_exit)
  414. sbi_platform_ops(plat)->ipi_exit();
  415. }
  416. /**
  417. * Initialize the platform timer for current HART
  418. *
  419. * @param plat pointer to struct sbi_platform
  420. * @param cold_boot whether cold boot (TRUE) or warm_boot (FALSE)
  421. *
  422. * @return 0 on success and negative error code on failure
  423. */
  424. static inline int sbi_platform_timer_init(const struct sbi_platform *plat,
  425. bool cold_boot)
  426. {
  427. if (plat && sbi_platform_ops(plat)->timer_init)
  428. return sbi_platform_ops(plat)->timer_init(cold_boot);
  429. return 0;
  430. }
  431. /**
  432. * Exit the platform timer for current HART
  433. *
  434. * @param plat pointer to struct sbi_platform
  435. */
  436. static inline void sbi_platform_timer_exit(const struct sbi_platform *plat)
  437. {
  438. if (plat && sbi_platform_ops(plat)->timer_exit)
  439. sbi_platform_ops(plat)->timer_exit();
  440. }
  441. /**
  442. * Check if a vendor extension is implemented or not.
  443. *
  444. * @param plat pointer to struct sbi_platform
  445. * @param extid vendor SBI extension id
  446. *
  447. * @return 0 if extid is not implemented and 1 if implemented
  448. */
  449. static inline int sbi_platform_vendor_ext_check(const struct sbi_platform *plat,
  450. long extid)
  451. {
  452. if (plat && sbi_platform_ops(plat)->vendor_ext_check)
  453. return sbi_platform_ops(plat)->vendor_ext_check(extid);
  454. return 0;
  455. }
  456. /**
  457. * Invoke platform specific vendor SBI extension implementation.
  458. *
  459. * @param plat pointer to struct sbi_platform
  460. * @param extid vendor SBI extension id
  461. * @param funcid SBI function id within the extension id
  462. * @param regs pointer to trap registers passed by the caller
  463. * @param out_value output value that can be filled by the callee
  464. * @param out_trap trap info that can be filled by the callee
  465. *
  466. * @return 0 on success and negative error code on failure
  467. */
  468. static inline int sbi_platform_vendor_ext_provider(
  469. const struct sbi_platform *plat,
  470. long extid, long funcid,
  471. const struct sbi_trap_regs *regs,
  472. unsigned long *out_value,
  473. struct sbi_trap_info *out_trap)
  474. {
  475. if (plat && sbi_platform_ops(plat)->vendor_ext_provider) {
  476. return sbi_platform_ops(plat)->vendor_ext_provider(extid,
  477. funcid, regs,
  478. out_value,
  479. out_trap);
  480. }
  481. return SBI_ENOTSUPP;
  482. }
  483. #endif
  484. #endif