rtas.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * Procedures for interfacing to the RTAS on CHRP machines.
  5. *
  6. * Peter Bergner, IBM March 2001.
  7. * Copyright (C) 2001 IBM.
  8. */
  9. #define pr_fmt(fmt) "rtas: " fmt
  10. #include <linux/bsearch.h>
  11. #include <linux/capability.h>
  12. #include <linux/delay.h>
  13. #include <linux/export.h>
  14. #include <linux/init.h>
  15. #include <linux/kconfig.h>
  16. #include <linux/kernel.h>
  17. #include <linux/lockdep.h>
  18. #include <linux/memblock.h>
  19. #include <linux/mutex.h>
  20. #include <linux/nospec.h>
  21. #include <linux/of.h>
  22. #include <linux/of_fdt.h>
  23. #include <linux/reboot.h>
  24. #include <linux/sched.h>
  25. #include <linux/security.h>
  26. #include <linux/slab.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/stdarg.h>
  29. #include <linux/syscalls.h>
  30. #include <linux/types.h>
  31. #include <linux/uaccess.h>
  32. #include <linux/xarray.h>
  33. #include <asm/delay.h>
  34. #include <asm/firmware.h>
  35. #include <asm/interrupt.h>
  36. #include <asm/machdep.h>
  37. #include <asm/mmu.h>
  38. #include <asm/page.h>
  39. #include <asm/rtas-work-area.h>
  40. #include <asm/rtas.h>
  41. #include <asm/time.h>
  42. #include <asm/trace.h>
  43. #include <asm/udbg.h>
  44. struct rtas_filter {
  45. /* Indexes into the args buffer, -1 if not used */
  46. const int buf_idx1;
  47. const int size_idx1;
  48. const int buf_idx2;
  49. const int size_idx2;
  50. /*
  51. * Assumed buffer size per the spec if the function does not
  52. * have a size parameter, e.g. ibm,errinjct. 0 if unused.
  53. */
  54. const int fixed_size;
  55. };
  56. /**
  57. * struct rtas_function - Descriptor for RTAS functions.
  58. *
  59. * @token: Value of @name if it exists under the /rtas node.
  60. * @name: Function name.
  61. * @filter: If non-NULL, invoking this function via the rtas syscall is
  62. * generally allowed, and @filter describes constraints on the
  63. * arguments. See also @banned_for_syscall_on_le.
  64. * @banned_for_syscall_on_le: Set when call via sys_rtas is generally allowed
  65. * but specifically restricted on ppc64le. Such
  66. * functions are believed to have no users on
  67. * ppc64le, and we want to keep it that way. It does
  68. * not make sense for this to be set when @filter
  69. * is NULL.
  70. * @lock: Pointer to an optional dedicated per-function mutex. This
  71. * should be set for functions that require multiple calls in
  72. * sequence to complete a single operation, and such sequences
  73. * will disrupt each other if allowed to interleave. Users of
  74. * this function are required to hold the associated lock for
  75. * the duration of the call sequence. Add an explanatory
  76. * comment to the function table entry if setting this member.
  77. */
  78. struct rtas_function {
  79. s32 token;
  80. const bool banned_for_syscall_on_le:1;
  81. const char * const name;
  82. const struct rtas_filter *filter;
  83. struct mutex *lock;
  84. };
  85. /*
  86. * Per-function locks for sequence-based RTAS functions.
  87. */
  88. static DEFINE_MUTEX(rtas_ibm_activate_firmware_lock);
  89. static DEFINE_MUTEX(rtas_ibm_get_dynamic_sensor_state_lock);
  90. static DEFINE_MUTEX(rtas_ibm_get_indices_lock);
  91. static DEFINE_MUTEX(rtas_ibm_lpar_perftools_lock);
  92. static DEFINE_MUTEX(rtas_ibm_physical_attestation_lock);
  93. static DEFINE_MUTEX(rtas_ibm_set_dynamic_indicator_lock);
  94. DEFINE_MUTEX(rtas_ibm_get_vpd_lock);
  95. static struct rtas_function rtas_function_table[] __ro_after_init = {
  96. [RTAS_FNIDX__CHECK_EXCEPTION] = {
  97. .name = "check-exception",
  98. },
  99. [RTAS_FNIDX__DISPLAY_CHARACTER] = {
  100. .name = "display-character",
  101. .filter = &(const struct rtas_filter) {
  102. .buf_idx1 = -1, .size_idx1 = -1,
  103. .buf_idx2 = -1, .size_idx2 = -1,
  104. },
  105. },
  106. [RTAS_FNIDX__EVENT_SCAN] = {
  107. .name = "event-scan",
  108. },
  109. [RTAS_FNIDX__FREEZE_TIME_BASE] = {
  110. .name = "freeze-time-base",
  111. },
  112. [RTAS_FNIDX__GET_POWER_LEVEL] = {
  113. .name = "get-power-level",
  114. .filter = &(const struct rtas_filter) {
  115. .buf_idx1 = -1, .size_idx1 = -1,
  116. .buf_idx2 = -1, .size_idx2 = -1,
  117. },
  118. },
  119. [RTAS_FNIDX__GET_SENSOR_STATE] = {
  120. .name = "get-sensor-state",
  121. .filter = &(const struct rtas_filter) {
  122. .buf_idx1 = -1, .size_idx1 = -1,
  123. .buf_idx2 = -1, .size_idx2 = -1,
  124. },
  125. },
  126. [RTAS_FNIDX__GET_TERM_CHAR] = {
  127. .name = "get-term-char",
  128. },
  129. [RTAS_FNIDX__GET_TIME_OF_DAY] = {
  130. .name = "get-time-of-day",
  131. .filter = &(const struct rtas_filter) {
  132. .buf_idx1 = -1, .size_idx1 = -1,
  133. .buf_idx2 = -1, .size_idx2 = -1,
  134. },
  135. },
  136. [RTAS_FNIDX__IBM_ACTIVATE_FIRMWARE] = {
  137. .name = "ibm,activate-firmware",
  138. .filter = &(const struct rtas_filter) {
  139. .buf_idx1 = -1, .size_idx1 = -1,
  140. .buf_idx2 = -1, .size_idx2 = -1,
  141. },
  142. /*
  143. * PAPR+ as of v2.13 doesn't explicitly impose any
  144. * restriction, but this typically requires multiple
  145. * calls before success, and there's no reason to
  146. * allow sequences to interleave.
  147. */
  148. .lock = &rtas_ibm_activate_firmware_lock,
  149. },
  150. [RTAS_FNIDX__IBM_CBE_START_PTCAL] = {
  151. .name = "ibm,cbe-start-ptcal",
  152. },
  153. [RTAS_FNIDX__IBM_CBE_STOP_PTCAL] = {
  154. .name = "ibm,cbe-stop-ptcal",
  155. },
  156. [RTAS_FNIDX__IBM_CHANGE_MSI] = {
  157. .name = "ibm,change-msi",
  158. },
  159. [RTAS_FNIDX__IBM_CLOSE_ERRINJCT] = {
  160. .name = "ibm,close-errinjct",
  161. .filter = &(const struct rtas_filter) {
  162. .buf_idx1 = -1, .size_idx1 = -1,
  163. .buf_idx2 = -1, .size_idx2 = -1,
  164. },
  165. },
  166. [RTAS_FNIDX__IBM_CONFIGURE_BRIDGE] = {
  167. .name = "ibm,configure-bridge",
  168. },
  169. [RTAS_FNIDX__IBM_CONFIGURE_CONNECTOR] = {
  170. .name = "ibm,configure-connector",
  171. .filter = &(const struct rtas_filter) {
  172. .buf_idx1 = 0, .size_idx1 = -1,
  173. .buf_idx2 = 1, .size_idx2 = -1,
  174. .fixed_size = 4096,
  175. },
  176. },
  177. [RTAS_FNIDX__IBM_CONFIGURE_KERNEL_DUMP] = {
  178. .name = "ibm,configure-kernel-dump",
  179. },
  180. [RTAS_FNIDX__IBM_CONFIGURE_PE] = {
  181. .name = "ibm,configure-pe",
  182. },
  183. [RTAS_FNIDX__IBM_CREATE_PE_DMA_WINDOW] = {
  184. .name = "ibm,create-pe-dma-window",
  185. },
  186. [RTAS_FNIDX__IBM_DISPLAY_MESSAGE] = {
  187. .name = "ibm,display-message",
  188. .filter = &(const struct rtas_filter) {
  189. .buf_idx1 = 0, .size_idx1 = -1,
  190. .buf_idx2 = -1, .size_idx2 = -1,
  191. },
  192. },
  193. [RTAS_FNIDX__IBM_ERRINJCT] = {
  194. .name = "ibm,errinjct",
  195. .filter = &(const struct rtas_filter) {
  196. .buf_idx1 = 2, .size_idx1 = -1,
  197. .buf_idx2 = -1, .size_idx2 = -1,
  198. .fixed_size = 1024,
  199. },
  200. },
  201. [RTAS_FNIDX__IBM_EXTI2C] = {
  202. .name = "ibm,exti2c",
  203. },
  204. [RTAS_FNIDX__IBM_GET_CONFIG_ADDR_INFO] = {
  205. .name = "ibm,get-config-addr-info",
  206. },
  207. [RTAS_FNIDX__IBM_GET_CONFIG_ADDR_INFO2] = {
  208. .name = "ibm,get-config-addr-info2",
  209. .filter = &(const struct rtas_filter) {
  210. .buf_idx1 = -1, .size_idx1 = -1,
  211. .buf_idx2 = -1, .size_idx2 = -1,
  212. },
  213. },
  214. [RTAS_FNIDX__IBM_GET_DYNAMIC_SENSOR_STATE] = {
  215. .name = "ibm,get-dynamic-sensor-state",
  216. .filter = &(const struct rtas_filter) {
  217. .buf_idx1 = 1, .size_idx1 = -1,
  218. .buf_idx2 = -1, .size_idx2 = -1,
  219. },
  220. /*
  221. * PAPR+ v2.13 R1–7.3.19–3 is explicit that the OS
  222. * must not call ibm,get-dynamic-sensor-state with
  223. * different inputs until a non-retry status has been
  224. * returned.
  225. */
  226. .lock = &rtas_ibm_get_dynamic_sensor_state_lock,
  227. },
  228. [RTAS_FNIDX__IBM_GET_INDICES] = {
  229. .name = "ibm,get-indices",
  230. .filter = &(const struct rtas_filter) {
  231. .buf_idx1 = 2, .size_idx1 = 3,
  232. .buf_idx2 = -1, .size_idx2 = -1,
  233. },
  234. /*
  235. * PAPR+ v2.13 R1–7.3.17–2 says that the OS must not
  236. * interleave ibm,get-indices call sequences with
  237. * different inputs.
  238. */
  239. .lock = &rtas_ibm_get_indices_lock,
  240. },
  241. [RTAS_FNIDX__IBM_GET_RIO_TOPOLOGY] = {
  242. .name = "ibm,get-rio-topology",
  243. },
  244. [RTAS_FNIDX__IBM_GET_SYSTEM_PARAMETER] = {
  245. .name = "ibm,get-system-parameter",
  246. .filter = &(const struct rtas_filter) {
  247. .buf_idx1 = 1, .size_idx1 = 2,
  248. .buf_idx2 = -1, .size_idx2 = -1,
  249. },
  250. },
  251. [RTAS_FNIDX__IBM_GET_VPD] = {
  252. .name = "ibm,get-vpd",
  253. .filter = &(const struct rtas_filter) {
  254. .buf_idx1 = 0, .size_idx1 = -1,
  255. .buf_idx2 = 1, .size_idx2 = 2,
  256. },
  257. /*
  258. * PAPR+ v2.13 R1–7.3.20–4 indicates that sequences
  259. * should not be allowed to interleave.
  260. */
  261. .lock = &rtas_ibm_get_vpd_lock,
  262. },
  263. [RTAS_FNIDX__IBM_GET_XIVE] = {
  264. .name = "ibm,get-xive",
  265. },
  266. [RTAS_FNIDX__IBM_INT_OFF] = {
  267. .name = "ibm,int-off",
  268. },
  269. [RTAS_FNIDX__IBM_INT_ON] = {
  270. .name = "ibm,int-on",
  271. },
  272. [RTAS_FNIDX__IBM_IO_QUIESCE_ACK] = {
  273. .name = "ibm,io-quiesce-ack",
  274. },
  275. [RTAS_FNIDX__IBM_LPAR_PERFTOOLS] = {
  276. .name = "ibm,lpar-perftools",
  277. .filter = &(const struct rtas_filter) {
  278. .buf_idx1 = 2, .size_idx1 = 3,
  279. .buf_idx2 = -1, .size_idx2 = -1,
  280. },
  281. /*
  282. * PAPR+ v2.13 R1–7.3.26–6 says the OS should allow
  283. * only one call sequence in progress at a time.
  284. */
  285. .lock = &rtas_ibm_lpar_perftools_lock,
  286. },
  287. [RTAS_FNIDX__IBM_MANAGE_FLASH_IMAGE] = {
  288. .name = "ibm,manage-flash-image",
  289. },
  290. [RTAS_FNIDX__IBM_MANAGE_STORAGE_PRESERVATION] = {
  291. .name = "ibm,manage-storage-preservation",
  292. },
  293. [RTAS_FNIDX__IBM_NMI_INTERLOCK] = {
  294. .name = "ibm,nmi-interlock",
  295. },
  296. [RTAS_FNIDX__IBM_NMI_REGISTER] = {
  297. .name = "ibm,nmi-register",
  298. },
  299. [RTAS_FNIDX__IBM_OPEN_ERRINJCT] = {
  300. .name = "ibm,open-errinjct",
  301. .filter = &(const struct rtas_filter) {
  302. .buf_idx1 = -1, .size_idx1 = -1,
  303. .buf_idx2 = -1, .size_idx2 = -1,
  304. },
  305. },
  306. [RTAS_FNIDX__IBM_OPEN_SRIOV_ALLOW_UNFREEZE] = {
  307. .name = "ibm,open-sriov-allow-unfreeze",
  308. },
  309. [RTAS_FNIDX__IBM_OPEN_SRIOV_MAP_PE_NUMBER] = {
  310. .name = "ibm,open-sriov-map-pe-number",
  311. },
  312. [RTAS_FNIDX__IBM_OS_TERM] = {
  313. .name = "ibm,os-term",
  314. },
  315. [RTAS_FNIDX__IBM_PARTNER_CONTROL] = {
  316. .name = "ibm,partner-control",
  317. },
  318. [RTAS_FNIDX__IBM_PHYSICAL_ATTESTATION] = {
  319. .name = "ibm,physical-attestation",
  320. .filter = &(const struct rtas_filter) {
  321. .buf_idx1 = 0, .size_idx1 = 1,
  322. .buf_idx2 = -1, .size_idx2 = -1,
  323. },
  324. /*
  325. * This follows a sequence-based pattern similar to
  326. * ibm,get-vpd et al. Since PAPR+ restricts
  327. * interleaving call sequences for other functions of
  328. * this style, assume the restriction applies here,
  329. * even though it's not explicit in the spec.
  330. */
  331. .lock = &rtas_ibm_physical_attestation_lock,
  332. },
  333. [RTAS_FNIDX__IBM_PLATFORM_DUMP] = {
  334. .name = "ibm,platform-dump",
  335. .filter = &(const struct rtas_filter) {
  336. .buf_idx1 = 4, .size_idx1 = 5,
  337. .buf_idx2 = -1, .size_idx2 = -1,
  338. },
  339. /*
  340. * PAPR+ v2.13 7.3.3.4.1 indicates that concurrent
  341. * sequences of ibm,platform-dump are allowed if they
  342. * are operating on different dump tags. So leave the
  343. * lock pointer unset for now. This may need
  344. * reconsideration if kernel-internal users appear.
  345. */
  346. },
  347. [RTAS_FNIDX__IBM_POWER_OFF_UPS] = {
  348. .name = "ibm,power-off-ups",
  349. },
  350. [RTAS_FNIDX__IBM_QUERY_INTERRUPT_SOURCE_NUMBER] = {
  351. .name = "ibm,query-interrupt-source-number",
  352. },
  353. [RTAS_FNIDX__IBM_QUERY_PE_DMA_WINDOW] = {
  354. .name = "ibm,query-pe-dma-window",
  355. },
  356. [RTAS_FNIDX__IBM_READ_PCI_CONFIG] = {
  357. .name = "ibm,read-pci-config",
  358. },
  359. [RTAS_FNIDX__IBM_READ_SLOT_RESET_STATE] = {
  360. .name = "ibm,read-slot-reset-state",
  361. .filter = &(const struct rtas_filter) {
  362. .buf_idx1 = -1, .size_idx1 = -1,
  363. .buf_idx2 = -1, .size_idx2 = -1,
  364. },
  365. },
  366. [RTAS_FNIDX__IBM_READ_SLOT_RESET_STATE2] = {
  367. .name = "ibm,read-slot-reset-state2",
  368. },
  369. [RTAS_FNIDX__IBM_REMOVE_PE_DMA_WINDOW] = {
  370. .name = "ibm,remove-pe-dma-window",
  371. },
  372. [RTAS_FNIDX__IBM_RESET_PE_DMA_WINDOW] = {
  373. /*
  374. * Note: PAPR+ v2.13 7.3.31.4.1 spells this as
  375. * "ibm,reset-pe-dma-windows" (plural), but RTAS
  376. * implementations use the singular form in practice.
  377. */
  378. .name = "ibm,reset-pe-dma-window",
  379. },
  380. [RTAS_FNIDX__IBM_SCAN_LOG_DUMP] = {
  381. .name = "ibm,scan-log-dump",
  382. .filter = &(const struct rtas_filter) {
  383. .buf_idx1 = 0, .size_idx1 = 1,
  384. .buf_idx2 = -1, .size_idx2 = -1,
  385. },
  386. },
  387. [RTAS_FNIDX__IBM_SET_DYNAMIC_INDICATOR] = {
  388. .name = "ibm,set-dynamic-indicator",
  389. .filter = &(const struct rtas_filter) {
  390. .buf_idx1 = 2, .size_idx1 = -1,
  391. .buf_idx2 = -1, .size_idx2 = -1,
  392. },
  393. /*
  394. * PAPR+ v2.13 R1–7.3.18–3 says the OS must not call
  395. * this function with different inputs until a
  396. * non-retry status has been returned.
  397. */
  398. .lock = &rtas_ibm_set_dynamic_indicator_lock,
  399. },
  400. [RTAS_FNIDX__IBM_SET_EEH_OPTION] = {
  401. .name = "ibm,set-eeh-option",
  402. .filter = &(const struct rtas_filter) {
  403. .buf_idx1 = -1, .size_idx1 = -1,
  404. .buf_idx2 = -1, .size_idx2 = -1,
  405. },
  406. },
  407. [RTAS_FNIDX__IBM_SET_SLOT_RESET] = {
  408. .name = "ibm,set-slot-reset",
  409. },
  410. [RTAS_FNIDX__IBM_SET_SYSTEM_PARAMETER] = {
  411. .name = "ibm,set-system-parameter",
  412. .filter = &(const struct rtas_filter) {
  413. .buf_idx1 = 1, .size_idx1 = -1,
  414. .buf_idx2 = -1, .size_idx2 = -1,
  415. },
  416. },
  417. [RTAS_FNIDX__IBM_SET_XIVE] = {
  418. .name = "ibm,set-xive",
  419. },
  420. [RTAS_FNIDX__IBM_SLOT_ERROR_DETAIL] = {
  421. .name = "ibm,slot-error-detail",
  422. },
  423. [RTAS_FNIDX__IBM_SUSPEND_ME] = {
  424. .name = "ibm,suspend-me",
  425. .banned_for_syscall_on_le = true,
  426. .filter = &(const struct rtas_filter) {
  427. .buf_idx1 = -1, .size_idx1 = -1,
  428. .buf_idx2 = -1, .size_idx2 = -1,
  429. },
  430. },
  431. [RTAS_FNIDX__IBM_TUNE_DMA_PARMS] = {
  432. .name = "ibm,tune-dma-parms",
  433. },
  434. [RTAS_FNIDX__IBM_UPDATE_FLASH_64_AND_REBOOT] = {
  435. .name = "ibm,update-flash-64-and-reboot",
  436. },
  437. [RTAS_FNIDX__IBM_UPDATE_NODES] = {
  438. .name = "ibm,update-nodes",
  439. .banned_for_syscall_on_le = true,
  440. .filter = &(const struct rtas_filter) {
  441. .buf_idx1 = 0, .size_idx1 = -1,
  442. .buf_idx2 = -1, .size_idx2 = -1,
  443. .fixed_size = 4096,
  444. },
  445. },
  446. [RTAS_FNIDX__IBM_UPDATE_PROPERTIES] = {
  447. .name = "ibm,update-properties",
  448. .banned_for_syscall_on_le = true,
  449. .filter = &(const struct rtas_filter) {
  450. .buf_idx1 = 0, .size_idx1 = -1,
  451. .buf_idx2 = -1, .size_idx2 = -1,
  452. .fixed_size = 4096,
  453. },
  454. },
  455. [RTAS_FNIDX__IBM_VALIDATE_FLASH_IMAGE] = {
  456. .name = "ibm,validate-flash-image",
  457. },
  458. [RTAS_FNIDX__IBM_WRITE_PCI_CONFIG] = {
  459. .name = "ibm,write-pci-config",
  460. },
  461. [RTAS_FNIDX__NVRAM_FETCH] = {
  462. .name = "nvram-fetch",
  463. },
  464. [RTAS_FNIDX__NVRAM_STORE] = {
  465. .name = "nvram-store",
  466. },
  467. [RTAS_FNIDX__POWER_OFF] = {
  468. .name = "power-off",
  469. },
  470. [RTAS_FNIDX__PUT_TERM_CHAR] = {
  471. .name = "put-term-char",
  472. },
  473. [RTAS_FNIDX__QUERY_CPU_STOPPED_STATE] = {
  474. .name = "query-cpu-stopped-state",
  475. },
  476. [RTAS_FNIDX__READ_PCI_CONFIG] = {
  477. .name = "read-pci-config",
  478. },
  479. [RTAS_FNIDX__RTAS_LAST_ERROR] = {
  480. .name = "rtas-last-error",
  481. },
  482. [RTAS_FNIDX__SET_INDICATOR] = {
  483. .name = "set-indicator",
  484. .filter = &(const struct rtas_filter) {
  485. .buf_idx1 = -1, .size_idx1 = -1,
  486. .buf_idx2 = -1, .size_idx2 = -1,
  487. },
  488. },
  489. [RTAS_FNIDX__SET_POWER_LEVEL] = {
  490. .name = "set-power-level",
  491. .filter = &(const struct rtas_filter) {
  492. .buf_idx1 = -1, .size_idx1 = -1,
  493. .buf_idx2 = -1, .size_idx2 = -1,
  494. },
  495. },
  496. [RTAS_FNIDX__SET_TIME_FOR_POWER_ON] = {
  497. .name = "set-time-for-power-on",
  498. .filter = &(const struct rtas_filter) {
  499. .buf_idx1 = -1, .size_idx1 = -1,
  500. .buf_idx2 = -1, .size_idx2 = -1,
  501. },
  502. },
  503. [RTAS_FNIDX__SET_TIME_OF_DAY] = {
  504. .name = "set-time-of-day",
  505. .filter = &(const struct rtas_filter) {
  506. .buf_idx1 = -1, .size_idx1 = -1,
  507. .buf_idx2 = -1, .size_idx2 = -1,
  508. },
  509. },
  510. [RTAS_FNIDX__START_CPU] = {
  511. .name = "start-cpu",
  512. },
  513. [RTAS_FNIDX__STOP_SELF] = {
  514. .name = "stop-self",
  515. },
  516. [RTAS_FNIDX__SYSTEM_REBOOT] = {
  517. .name = "system-reboot",
  518. },
  519. [RTAS_FNIDX__THAW_TIME_BASE] = {
  520. .name = "thaw-time-base",
  521. },
  522. [RTAS_FNIDX__WRITE_PCI_CONFIG] = {
  523. .name = "write-pci-config",
  524. },
  525. };
  526. #define for_each_rtas_function(funcp) \
  527. for (funcp = &rtas_function_table[0]; \
  528. funcp < &rtas_function_table[ARRAY_SIZE(rtas_function_table)]; \
  529. ++funcp)
  530. /*
  531. * Nearly all RTAS calls need to be serialized. All uses of the
  532. * default rtas_args block must hold rtas_lock.
  533. *
  534. * Exceptions to the RTAS serialization requirement (e.g. stop-self)
  535. * must use a separate rtas_args structure.
  536. */
  537. static DEFINE_RAW_SPINLOCK(rtas_lock);
  538. static struct rtas_args rtas_args;
  539. /**
  540. * rtas_function_token() - RTAS function token lookup.
  541. * @handle: Function handle, e.g. RTAS_FN_EVENT_SCAN.
  542. *
  543. * Context: Any context.
  544. * Return: the token value for the function if implemented by this platform,
  545. * otherwise RTAS_UNKNOWN_SERVICE.
  546. */
  547. s32 rtas_function_token(const rtas_fn_handle_t handle)
  548. {
  549. const size_t index = handle.index;
  550. const bool out_of_bounds = index >= ARRAY_SIZE(rtas_function_table);
  551. if (WARN_ONCE(out_of_bounds, "invalid function index %zu", index))
  552. return RTAS_UNKNOWN_SERVICE;
  553. /*
  554. * Various drivers attempt token lookups on non-RTAS
  555. * platforms.
  556. */
  557. if (!rtas.dev)
  558. return RTAS_UNKNOWN_SERVICE;
  559. return rtas_function_table[index].token;
  560. }
  561. EXPORT_SYMBOL_GPL(rtas_function_token);
  562. static int rtas_function_cmp(const void *a, const void *b)
  563. {
  564. const struct rtas_function *f1 = a;
  565. const struct rtas_function *f2 = b;
  566. return strcmp(f1->name, f2->name);
  567. }
  568. /*
  569. * Boot-time initialization of the function table needs the lookup to
  570. * return a non-const-qualified object. Use rtas_name_to_function()
  571. * in all other contexts.
  572. */
  573. static struct rtas_function *__rtas_name_to_function(const char *name)
  574. {
  575. const struct rtas_function key = {
  576. .name = name,
  577. };
  578. struct rtas_function *found;
  579. found = bsearch(&key, rtas_function_table, ARRAY_SIZE(rtas_function_table),
  580. sizeof(rtas_function_table[0]), rtas_function_cmp);
  581. return found;
  582. }
  583. static const struct rtas_function *rtas_name_to_function(const char *name)
  584. {
  585. return __rtas_name_to_function(name);
  586. }
  587. static DEFINE_XARRAY(rtas_token_to_function_xarray);
  588. static int __init rtas_token_to_function_xarray_init(void)
  589. {
  590. const struct rtas_function *func;
  591. int err = 0;
  592. for_each_rtas_function(func) {
  593. const s32 token = func->token;
  594. if (token == RTAS_UNKNOWN_SERVICE)
  595. continue;
  596. err = xa_err(xa_store(&rtas_token_to_function_xarray,
  597. token, (void *)func, GFP_KERNEL));
  598. if (err)
  599. break;
  600. }
  601. return err;
  602. }
  603. arch_initcall(rtas_token_to_function_xarray_init);
  604. /*
  605. * For use by sys_rtas(), where the token value is provided by user
  606. * space and we don't want to warn on failed lookups.
  607. */
  608. static const struct rtas_function *rtas_token_to_function_untrusted(s32 token)
  609. {
  610. return xa_load(&rtas_token_to_function_xarray, token);
  611. }
  612. /*
  613. * Reverse lookup for deriving the function descriptor from a
  614. * known-good token value in contexts where the former is not already
  615. * available. @token must be valid, e.g. derived from the result of a
  616. * prior lookup against the function table.
  617. */
  618. static const struct rtas_function *rtas_token_to_function(s32 token)
  619. {
  620. const struct rtas_function *func;
  621. if (WARN_ONCE(token < 0, "invalid token %d", token))
  622. return NULL;
  623. func = rtas_token_to_function_untrusted(token);
  624. if (func)
  625. return func;
  626. /*
  627. * Fall back to linear scan in case the reverse mapping hasn't
  628. * been initialized yet.
  629. */
  630. if (xa_empty(&rtas_token_to_function_xarray)) {
  631. for_each_rtas_function(func) {
  632. if (func->token == token)
  633. return func;
  634. }
  635. }
  636. WARN_ONCE(true, "unexpected failed lookup for token %d", token);
  637. return NULL;
  638. }
  639. /* This is here deliberately so it's only used in this file */
  640. void enter_rtas(unsigned long);
  641. static void __do_enter_rtas(struct rtas_args *args)
  642. {
  643. enter_rtas(__pa(args));
  644. srr_regs_clobbered(); /* rtas uses SRRs, invalidate */
  645. }
  646. static void __do_enter_rtas_trace(struct rtas_args *args)
  647. {
  648. const struct rtas_function *func = rtas_token_to_function(be32_to_cpu(args->token));
  649. /*
  650. * If there is a per-function lock, it must be held by the
  651. * caller.
  652. */
  653. if (func->lock)
  654. lockdep_assert_held(func->lock);
  655. if (args == &rtas_args)
  656. lockdep_assert_held(&rtas_lock);
  657. trace_rtas_input(args, func->name);
  658. trace_rtas_ll_entry(args);
  659. __do_enter_rtas(args);
  660. trace_rtas_ll_exit(args);
  661. trace_rtas_output(args, func->name);
  662. }
  663. static void do_enter_rtas(struct rtas_args *args)
  664. {
  665. const unsigned long msr = mfmsr();
  666. /*
  667. * Situations where we want to skip any active tracepoints for
  668. * safety reasons:
  669. *
  670. * 1. The last code executed on an offline CPU as it stops,
  671. * i.e. we're about to call stop-self. The tracepoints'
  672. * function name lookup uses xarray, which uses RCU, which
  673. * isn't valid to call on an offline CPU. Any events
  674. * emitted on an offline CPU will be discarded anyway.
  675. *
  676. * 2. In real mode, as when invoking ibm,nmi-interlock from
  677. * the pseries MCE handler. We cannot count on trace
  678. * buffers or the entries in rtas_token_to_function_xarray
  679. * to be contained in the RMO.
  680. */
  681. const unsigned long mask = MSR_IR | MSR_DR;
  682. const bool can_trace = likely(cpu_online(raw_smp_processor_id()) &&
  683. (msr & mask) == mask);
  684. /*
  685. * Make sure MSR[RI] is currently enabled as it will be forced later
  686. * in enter_rtas.
  687. */
  688. BUG_ON(!(msr & MSR_RI));
  689. BUG_ON(!irqs_disabled());
  690. hard_irq_disable(); /* Ensure MSR[EE] is disabled on PPC64 */
  691. if (can_trace)
  692. __do_enter_rtas_trace(args);
  693. else
  694. __do_enter_rtas(args);
  695. }
  696. struct rtas_t rtas;
  697. DEFINE_SPINLOCK(rtas_data_buf_lock);
  698. EXPORT_SYMBOL_GPL(rtas_data_buf_lock);
  699. char rtas_data_buf[RTAS_DATA_BUF_SIZE] __aligned(SZ_4K);
  700. EXPORT_SYMBOL_GPL(rtas_data_buf);
  701. unsigned long rtas_rmo_buf;
  702. /*
  703. * If non-NULL, this gets called when the kernel terminates.
  704. * This is done like this so rtas_flash can be a module.
  705. */
  706. void (*rtas_flash_term_hook)(int);
  707. EXPORT_SYMBOL_GPL(rtas_flash_term_hook);
  708. /*
  709. * call_rtas_display_status and call_rtas_display_status_delay
  710. * are designed only for very early low-level debugging, which
  711. * is why the token is hard-coded to 10.
  712. */
  713. static void call_rtas_display_status(unsigned char c)
  714. {
  715. unsigned long flags;
  716. if (!rtas.base)
  717. return;
  718. raw_spin_lock_irqsave(&rtas_lock, flags);
  719. rtas_call_unlocked(&rtas_args, 10, 1, 1, NULL, c);
  720. raw_spin_unlock_irqrestore(&rtas_lock, flags);
  721. }
  722. static void call_rtas_display_status_delay(char c)
  723. {
  724. static int pending_newline = 0; /* did last write end with unprinted newline? */
  725. static int width = 16;
  726. if (c == '\n') {
  727. while (width-- > 0)
  728. call_rtas_display_status(' ');
  729. width = 16;
  730. mdelay(500);
  731. pending_newline = 1;
  732. } else {
  733. if (pending_newline) {
  734. call_rtas_display_status('\r');
  735. call_rtas_display_status('\n');
  736. }
  737. pending_newline = 0;
  738. if (width--) {
  739. call_rtas_display_status(c);
  740. udelay(10000);
  741. }
  742. }
  743. }
  744. void __init udbg_init_rtas_panel(void)
  745. {
  746. udbg_putc = call_rtas_display_status_delay;
  747. }
  748. #ifdef CONFIG_UDBG_RTAS_CONSOLE
  749. /* If you think you're dying before early_init_dt_scan_rtas() does its
  750. * work, you can hard code the token values for your firmware here and
  751. * hardcode rtas.base/entry etc.
  752. */
  753. static unsigned int rtas_putchar_token = RTAS_UNKNOWN_SERVICE;
  754. static unsigned int rtas_getchar_token = RTAS_UNKNOWN_SERVICE;
  755. static void udbg_rtascon_putc(char c)
  756. {
  757. int tries;
  758. if (!rtas.base)
  759. return;
  760. /* Add CRs before LFs */
  761. if (c == '\n')
  762. udbg_rtascon_putc('\r');
  763. /* if there is more than one character to be displayed, wait a bit */
  764. for (tries = 0; tries < 16; tries++) {
  765. if (rtas_call(rtas_putchar_token, 1, 1, NULL, c) == 0)
  766. break;
  767. udelay(1000);
  768. }
  769. }
  770. static int udbg_rtascon_getc_poll(void)
  771. {
  772. int c;
  773. if (!rtas.base)
  774. return -1;
  775. if (rtas_call(rtas_getchar_token, 0, 2, &c))
  776. return -1;
  777. return c;
  778. }
  779. static int udbg_rtascon_getc(void)
  780. {
  781. int c;
  782. while ((c = udbg_rtascon_getc_poll()) == -1)
  783. ;
  784. return c;
  785. }
  786. void __init udbg_init_rtas_console(void)
  787. {
  788. udbg_putc = udbg_rtascon_putc;
  789. udbg_getc = udbg_rtascon_getc;
  790. udbg_getc_poll = udbg_rtascon_getc_poll;
  791. }
  792. #endif /* CONFIG_UDBG_RTAS_CONSOLE */
  793. void rtas_progress(char *s, unsigned short hex)
  794. {
  795. struct device_node *root;
  796. int width;
  797. const __be32 *p;
  798. char *os;
  799. static int display_character, set_indicator;
  800. static int display_width, display_lines, form_feed;
  801. static const int *row_width;
  802. static DEFINE_SPINLOCK(progress_lock);
  803. static int current_line;
  804. static int pending_newline = 0; /* did last write end with unprinted newline? */
  805. if (!rtas.base)
  806. return;
  807. if (display_width == 0) {
  808. display_width = 0x10;
  809. if ((root = of_find_node_by_path("/rtas"))) {
  810. if ((p = of_get_property(root,
  811. "ibm,display-line-length", NULL)))
  812. display_width = be32_to_cpu(*p);
  813. if ((p = of_get_property(root,
  814. "ibm,form-feed", NULL)))
  815. form_feed = be32_to_cpu(*p);
  816. if ((p = of_get_property(root,
  817. "ibm,display-number-of-lines", NULL)))
  818. display_lines = be32_to_cpu(*p);
  819. row_width = of_get_property(root,
  820. "ibm,display-truncation-length", NULL);
  821. of_node_put(root);
  822. }
  823. display_character = rtas_function_token(RTAS_FN_DISPLAY_CHARACTER);
  824. set_indicator = rtas_function_token(RTAS_FN_SET_INDICATOR);
  825. }
  826. if (display_character == RTAS_UNKNOWN_SERVICE) {
  827. /* use hex display if available */
  828. if (set_indicator != RTAS_UNKNOWN_SERVICE)
  829. rtas_call(set_indicator, 3, 1, NULL, 6, 0, hex);
  830. return;
  831. }
  832. spin_lock(&progress_lock);
  833. /*
  834. * Last write ended with newline, but we didn't print it since
  835. * it would just clear the bottom line of output. Print it now
  836. * instead.
  837. *
  838. * If no newline is pending and form feed is supported, clear the
  839. * display with a form feed; otherwise, print a CR to start output
  840. * at the beginning of the line.
  841. */
  842. if (pending_newline) {
  843. rtas_call(display_character, 1, 1, NULL, '\r');
  844. rtas_call(display_character, 1, 1, NULL, '\n');
  845. pending_newline = 0;
  846. } else {
  847. current_line = 0;
  848. if (form_feed)
  849. rtas_call(display_character, 1, 1, NULL,
  850. (char)form_feed);
  851. else
  852. rtas_call(display_character, 1, 1, NULL, '\r');
  853. }
  854. if (row_width)
  855. width = row_width[current_line];
  856. else
  857. width = display_width;
  858. os = s;
  859. while (*os) {
  860. if (*os == '\n' || *os == '\r') {
  861. /* If newline is the last character, save it
  862. * until next call to avoid bumping up the
  863. * display output.
  864. */
  865. if (*os == '\n' && !os[1]) {
  866. pending_newline = 1;
  867. current_line++;
  868. if (current_line > display_lines-1)
  869. current_line = display_lines-1;
  870. spin_unlock(&progress_lock);
  871. return;
  872. }
  873. /* RTAS wants CR-LF, not just LF */
  874. if (*os == '\n') {
  875. rtas_call(display_character, 1, 1, NULL, '\r');
  876. rtas_call(display_character, 1, 1, NULL, '\n');
  877. } else {
  878. /* CR might be used to re-draw a line, so we'll
  879. * leave it alone and not add LF.
  880. */
  881. rtas_call(display_character, 1, 1, NULL, *os);
  882. }
  883. if (row_width)
  884. width = row_width[current_line];
  885. else
  886. width = display_width;
  887. } else {
  888. width--;
  889. rtas_call(display_character, 1, 1, NULL, *os);
  890. }
  891. os++;
  892. /* if we overwrite the screen length */
  893. if (width <= 0)
  894. while ((*os != 0) && (*os != '\n') && (*os != '\r'))
  895. os++;
  896. }
  897. spin_unlock(&progress_lock);
  898. }
  899. EXPORT_SYMBOL_GPL(rtas_progress); /* needed by rtas_flash module */
  900. int rtas_token(const char *service)
  901. {
  902. const struct rtas_function *func;
  903. const __be32 *tokp;
  904. if (rtas.dev == NULL)
  905. return RTAS_UNKNOWN_SERVICE;
  906. func = rtas_name_to_function(service);
  907. if (func)
  908. return func->token;
  909. /*
  910. * The caller is looking up a name that is not known to be an
  911. * RTAS function. Either it's a function that needs to be
  912. * added to the table, or they're misusing rtas_token() to
  913. * access non-function properties of the /rtas node. Warn and
  914. * fall back to the legacy behavior.
  915. */
  916. WARN_ONCE(1, "unknown function `%s`, should it be added to rtas_function_table?\n",
  917. service);
  918. tokp = of_get_property(rtas.dev, service, NULL);
  919. return tokp ? be32_to_cpu(*tokp) : RTAS_UNKNOWN_SERVICE;
  920. }
  921. EXPORT_SYMBOL_GPL(rtas_token);
  922. #ifdef CONFIG_RTAS_ERROR_LOGGING
  923. static u32 rtas_error_log_max __ro_after_init = RTAS_ERROR_LOG_MAX;
  924. /*
  925. * Return the firmware-specified size of the error log buffer
  926. * for all rtas calls that require an error buffer argument.
  927. * This includes 'check-exception' and 'rtas-last-error'.
  928. */
  929. int rtas_get_error_log_max(void)
  930. {
  931. return rtas_error_log_max;
  932. }
  933. static void __init init_error_log_max(void)
  934. {
  935. static const char propname[] __initconst = "rtas-error-log-max";
  936. u32 max;
  937. if (of_property_read_u32(rtas.dev, propname, &max)) {
  938. pr_warn("%s not found, using default of %u\n",
  939. propname, RTAS_ERROR_LOG_MAX);
  940. max = RTAS_ERROR_LOG_MAX;
  941. }
  942. if (max > RTAS_ERROR_LOG_MAX) {
  943. pr_warn("%s = %u, clamping max error log size to %u\n",
  944. propname, max, RTAS_ERROR_LOG_MAX);
  945. max = RTAS_ERROR_LOG_MAX;
  946. }
  947. rtas_error_log_max = max;
  948. }
  949. static char rtas_err_buf[RTAS_ERROR_LOG_MAX];
  950. /** Return a copy of the detailed error text associated with the
  951. * most recent failed call to rtas. Because the error text
  952. * might go stale if there are any other intervening rtas calls,
  953. * this routine must be called atomically with whatever produced
  954. * the error (i.e. with rtas_lock still held from the previous call).
  955. */
  956. static char *__fetch_rtas_last_error(char *altbuf)
  957. {
  958. const s32 token = rtas_function_token(RTAS_FN_RTAS_LAST_ERROR);
  959. struct rtas_args err_args, save_args;
  960. u32 bufsz;
  961. char *buf = NULL;
  962. lockdep_assert_held(&rtas_lock);
  963. if (token == -1)
  964. return NULL;
  965. bufsz = rtas_get_error_log_max();
  966. err_args.token = cpu_to_be32(token);
  967. err_args.nargs = cpu_to_be32(2);
  968. err_args.nret = cpu_to_be32(1);
  969. err_args.args[0] = cpu_to_be32(__pa(rtas_err_buf));
  970. err_args.args[1] = cpu_to_be32(bufsz);
  971. err_args.args[2] = 0;
  972. save_args = rtas_args;
  973. rtas_args = err_args;
  974. do_enter_rtas(&rtas_args);
  975. err_args = rtas_args;
  976. rtas_args = save_args;
  977. /* Log the error in the unlikely case that there was one. */
  978. if (unlikely(err_args.args[2] == 0)) {
  979. if (altbuf) {
  980. buf = altbuf;
  981. } else {
  982. buf = rtas_err_buf;
  983. if (slab_is_available())
  984. buf = kmalloc(RTAS_ERROR_LOG_MAX, GFP_ATOMIC);
  985. }
  986. if (buf)
  987. memmove(buf, rtas_err_buf, RTAS_ERROR_LOG_MAX);
  988. }
  989. return buf;
  990. }
  991. #define get_errorlog_buffer() kmalloc(RTAS_ERROR_LOG_MAX, GFP_KERNEL)
  992. #else /* CONFIG_RTAS_ERROR_LOGGING */
  993. #define __fetch_rtas_last_error(x) NULL
  994. #define get_errorlog_buffer() NULL
  995. static void __init init_error_log_max(void) {}
  996. #endif
  997. static void
  998. va_rtas_call_unlocked(struct rtas_args *args, int token, int nargs, int nret,
  999. va_list list)
  1000. {
  1001. int i;
  1002. args->token = cpu_to_be32(token);
  1003. args->nargs = cpu_to_be32(nargs);
  1004. args->nret = cpu_to_be32(nret);
  1005. args->rets = &(args->args[nargs]);
  1006. for (i = 0; i < nargs; ++i)
  1007. args->args[i] = cpu_to_be32(va_arg(list, __u32));
  1008. for (i = 0; i < nret; ++i)
  1009. args->rets[i] = 0;
  1010. do_enter_rtas(args);
  1011. }
  1012. /**
  1013. * rtas_call_unlocked() - Invoke an RTAS firmware function without synchronization.
  1014. * @args: RTAS parameter block to be used for the call, must obey RTAS addressing
  1015. * constraints.
  1016. * @token: Identifies the function being invoked.
  1017. * @nargs: Number of input parameters. Does not include token.
  1018. * @nret: Number of output parameters, including the call status.
  1019. * @....: List of @nargs input parameters.
  1020. *
  1021. * Invokes the RTAS function indicated by @token, which the caller
  1022. * should obtain via rtas_function_token().
  1023. *
  1024. * This function is similar to rtas_call(), but must be used with a
  1025. * limited set of RTAS calls specifically exempted from the general
  1026. * requirement that only one RTAS call may be in progress at any
  1027. * time. Examples include stop-self and ibm,nmi-interlock.
  1028. */
  1029. void rtas_call_unlocked(struct rtas_args *args, int token, int nargs, int nret, ...)
  1030. {
  1031. va_list list;
  1032. va_start(list, nret);
  1033. va_rtas_call_unlocked(args, token, nargs, nret, list);
  1034. va_end(list);
  1035. }
  1036. static bool token_is_restricted_errinjct(s32 token)
  1037. {
  1038. return token == rtas_function_token(RTAS_FN_IBM_OPEN_ERRINJCT) ||
  1039. token == rtas_function_token(RTAS_FN_IBM_ERRINJCT);
  1040. }
  1041. /**
  1042. * rtas_call() - Invoke an RTAS firmware function.
  1043. * @token: Identifies the function being invoked.
  1044. * @nargs: Number of input parameters. Does not include token.
  1045. * @nret: Number of output parameters, including the call status.
  1046. * @outputs: Array of @nret output words.
  1047. * @....: List of @nargs input parameters.
  1048. *
  1049. * Invokes the RTAS function indicated by @token, which the caller
  1050. * should obtain via rtas_function_token().
  1051. *
  1052. * The @nargs and @nret arguments must match the number of input and
  1053. * output parameters specified for the RTAS function.
  1054. *
  1055. * rtas_call() returns RTAS status codes, not conventional Linux errno
  1056. * values. Callers must translate any failure to an appropriate errno
  1057. * in syscall context. Most callers of RTAS functions that can return
  1058. * -2 or 990x should use rtas_busy_delay() to correctly handle those
  1059. * statuses before calling again.
  1060. *
  1061. * The return value descriptions are adapted from 7.2.8 [RTAS] Return
  1062. * Codes of the PAPR and CHRP specifications.
  1063. *
  1064. * Context: Process context preferably, interrupt context if
  1065. * necessary. Acquires an internal spinlock and may perform
  1066. * GFP_ATOMIC slab allocation in error path. Unsafe for NMI
  1067. * context.
  1068. * Return:
  1069. * * 0 - RTAS function call succeeded.
  1070. * * -1 - RTAS function encountered a hardware or
  1071. * platform error, or the token is invalid,
  1072. * or the function is restricted by kernel policy.
  1073. * * -2 - Specs say "A necessary hardware device was busy,
  1074. * and the requested function could not be
  1075. * performed. The operation should be retried at
  1076. * a later time." This is misleading, at least with
  1077. * respect to current RTAS implementations. What it
  1078. * usually means in practice is that the function
  1079. * could not be completed while meeting RTAS's
  1080. * deadline for returning control to the OS (250us
  1081. * for PAPR/PowerVM, typically), but the call may be
  1082. * immediately reattempted to resume work on it.
  1083. * * -3 - Parameter error.
  1084. * * -7 - Unexpected state change.
  1085. * * 9000...9899 - Vendor-specific success codes.
  1086. * * 9900...9905 - Advisory extended delay. Caller should try
  1087. * again after ~10^x ms has elapsed, where x is
  1088. * the last digit of the status [0-5]. Again going
  1089. * beyond the PAPR text, 990x on PowerVM indicates
  1090. * contention for RTAS-internal resources. Other
  1091. * RTAS call sequences in progress should be
  1092. * allowed to complete before reattempting the
  1093. * call.
  1094. * * -9000 - Multi-level isolation error.
  1095. * * -9999...-9004 - Vendor-specific error codes.
  1096. * * Additional negative values - Function-specific error.
  1097. * * Additional positive values - Function-specific success.
  1098. */
  1099. int rtas_call(int token, int nargs, int nret, int *outputs, ...)
  1100. {
  1101. struct pin_cookie cookie;
  1102. va_list list;
  1103. int i;
  1104. unsigned long flags;
  1105. struct rtas_args *args;
  1106. char *buff_copy = NULL;
  1107. int ret;
  1108. if (!rtas.entry || token == RTAS_UNKNOWN_SERVICE)
  1109. return -1;
  1110. if (token_is_restricted_errinjct(token)) {
  1111. /*
  1112. * It would be nicer to not discard the error value
  1113. * from security_locked_down(), but callers expect an
  1114. * RTAS status, not an errno.
  1115. */
  1116. if (security_locked_down(LOCKDOWN_RTAS_ERROR_INJECTION))
  1117. return -1;
  1118. }
  1119. if ((mfmsr() & (MSR_IR|MSR_DR)) != (MSR_IR|MSR_DR)) {
  1120. WARN_ON_ONCE(1);
  1121. return -1;
  1122. }
  1123. raw_spin_lock_irqsave(&rtas_lock, flags);
  1124. cookie = lockdep_pin_lock(&rtas_lock);
  1125. /* We use the global rtas args buffer */
  1126. args = &rtas_args;
  1127. va_start(list, outputs);
  1128. va_rtas_call_unlocked(args, token, nargs, nret, list);
  1129. va_end(list);
  1130. /* A -1 return code indicates that the last command couldn't
  1131. be completed due to a hardware error. */
  1132. if (be32_to_cpu(args->rets[0]) == -1)
  1133. buff_copy = __fetch_rtas_last_error(NULL);
  1134. if (nret > 1 && outputs != NULL)
  1135. for (i = 0; i < nret-1; ++i)
  1136. outputs[i] = be32_to_cpu(args->rets[i + 1]);
  1137. ret = (nret > 0) ? be32_to_cpu(args->rets[0]) : 0;
  1138. lockdep_unpin_lock(&rtas_lock, cookie);
  1139. raw_spin_unlock_irqrestore(&rtas_lock, flags);
  1140. if (buff_copy) {
  1141. log_error(buff_copy, ERR_TYPE_RTAS_LOG, 0);
  1142. if (slab_is_available())
  1143. kfree(buff_copy);
  1144. }
  1145. return ret;
  1146. }
  1147. EXPORT_SYMBOL_GPL(rtas_call);
  1148. /**
  1149. * rtas_busy_delay_time() - From an RTAS status value, calculate the
  1150. * suggested delay time in milliseconds.
  1151. *
  1152. * @status: a value returned from rtas_call() or similar APIs which return
  1153. * the status of a RTAS function call.
  1154. *
  1155. * Context: Any context.
  1156. *
  1157. * Return:
  1158. * * 100000 - If @status is 9905.
  1159. * * 10000 - If @status is 9904.
  1160. * * 1000 - If @status is 9903.
  1161. * * 100 - If @status is 9902.
  1162. * * 10 - If @status is 9901.
  1163. * * 1 - If @status is either 9900 or -2. This is "wrong" for -2, but
  1164. * some callers depend on this behavior, and the worst outcome
  1165. * is that they will delay for longer than necessary.
  1166. * * 0 - If @status is not a busy or extended delay value.
  1167. */
  1168. unsigned int rtas_busy_delay_time(int status)
  1169. {
  1170. int order;
  1171. unsigned int ms = 0;
  1172. if (status == RTAS_BUSY) {
  1173. ms = 1;
  1174. } else if (status >= RTAS_EXTENDED_DELAY_MIN &&
  1175. status <= RTAS_EXTENDED_DELAY_MAX) {
  1176. order = status - RTAS_EXTENDED_DELAY_MIN;
  1177. for (ms = 1; order > 0; order--)
  1178. ms *= 10;
  1179. }
  1180. return ms;
  1181. }
  1182. /*
  1183. * Early boot fallback for rtas_busy_delay().
  1184. */
  1185. static bool __init rtas_busy_delay_early(int status)
  1186. {
  1187. static size_t successive_ext_delays __initdata;
  1188. bool retry;
  1189. switch (status) {
  1190. case RTAS_EXTENDED_DELAY_MIN...RTAS_EXTENDED_DELAY_MAX:
  1191. /*
  1192. * In the unlikely case that we receive an extended
  1193. * delay status in early boot, the OS is probably not
  1194. * the cause, and there's nothing we can do to clear
  1195. * the condition. Best we can do is delay for a bit
  1196. * and hope it's transient. Lie to the caller if it
  1197. * seems like we're stuck in a retry loop.
  1198. */
  1199. mdelay(1);
  1200. retry = true;
  1201. successive_ext_delays += 1;
  1202. if (successive_ext_delays > 1000) {
  1203. pr_err("too many extended delays, giving up\n");
  1204. dump_stack();
  1205. retry = false;
  1206. successive_ext_delays = 0;
  1207. }
  1208. break;
  1209. case RTAS_BUSY:
  1210. retry = true;
  1211. successive_ext_delays = 0;
  1212. break;
  1213. default:
  1214. retry = false;
  1215. successive_ext_delays = 0;
  1216. break;
  1217. }
  1218. return retry;
  1219. }
  1220. /**
  1221. * rtas_busy_delay() - helper for RTAS busy and extended delay statuses
  1222. *
  1223. * @status: a value returned from rtas_call() or similar APIs which return
  1224. * the status of a RTAS function call.
  1225. *
  1226. * Context: Process context. May sleep or schedule.
  1227. *
  1228. * Return:
  1229. * * true - @status is RTAS_BUSY or an extended delay hint. The
  1230. * caller may assume that the CPU has been yielded if necessary,
  1231. * and that an appropriate delay for @status has elapsed.
  1232. * Generally the caller should reattempt the RTAS call which
  1233. * yielded @status.
  1234. *
  1235. * * false - @status is not @RTAS_BUSY nor an extended delay hint. The
  1236. * caller is responsible for handling @status.
  1237. */
  1238. bool __ref rtas_busy_delay(int status)
  1239. {
  1240. unsigned int ms;
  1241. bool ret;
  1242. /*
  1243. * Can't do timed sleeps before timekeeping is up.
  1244. */
  1245. if (system_state < SYSTEM_SCHEDULING)
  1246. return rtas_busy_delay_early(status);
  1247. switch (status) {
  1248. case RTAS_EXTENDED_DELAY_MIN...RTAS_EXTENDED_DELAY_MAX:
  1249. ret = true;
  1250. ms = rtas_busy_delay_time(status);
  1251. /*
  1252. * The extended delay hint can be as high as 100 seconds.
  1253. * Surely any function returning such a status is either
  1254. * buggy or isn't going to be significantly slowed by us
  1255. * polling at 1HZ. Clamp the sleep time to one second.
  1256. */
  1257. ms = clamp(ms, 1U, 1000U);
  1258. /*
  1259. * The delay hint is an order-of-magnitude suggestion, not
  1260. * a minimum. It is fine, possibly even advantageous, for
  1261. * us to pause for less time than hinted. For small values,
  1262. * use usleep_range() to ensure we don't sleep much longer
  1263. * than actually needed.
  1264. *
  1265. * See Documentation/timers/timers-howto.rst for
  1266. * explanation of the threshold used here. In effect we use
  1267. * usleep_range() for 9900 and 9901, msleep() for
  1268. * 9902-9905.
  1269. */
  1270. if (ms <= 20)
  1271. usleep_range(ms * 100, ms * 1000);
  1272. else
  1273. msleep(ms);
  1274. break;
  1275. case RTAS_BUSY:
  1276. ret = true;
  1277. /*
  1278. * We should call again immediately if there's no other
  1279. * work to do.
  1280. */
  1281. cond_resched();
  1282. break;
  1283. default:
  1284. ret = false;
  1285. /*
  1286. * Not a busy or extended delay status; the caller should
  1287. * handle @status itself. Ensure we warn on misuses in
  1288. * atomic context regardless.
  1289. */
  1290. might_sleep();
  1291. break;
  1292. }
  1293. return ret;
  1294. }
  1295. EXPORT_SYMBOL_GPL(rtas_busy_delay);
  1296. int rtas_error_rc(int rtas_rc)
  1297. {
  1298. int rc;
  1299. switch (rtas_rc) {
  1300. case RTAS_HARDWARE_ERROR: /* Hardware Error */
  1301. rc = -EIO;
  1302. break;
  1303. case RTAS_INVALID_PARAMETER: /* Bad indicator/domain/etc */
  1304. rc = -EINVAL;
  1305. break;
  1306. case -9000: /* Isolation error */
  1307. rc = -EFAULT;
  1308. break;
  1309. case -9001: /* Outstanding TCE/PTE */
  1310. rc = -EEXIST;
  1311. break;
  1312. case -9002: /* No usable slot */
  1313. rc = -ENODEV;
  1314. break;
  1315. default:
  1316. pr_err("%s: unexpected error %d\n", __func__, rtas_rc);
  1317. rc = -ERANGE;
  1318. break;
  1319. }
  1320. return rc;
  1321. }
  1322. EXPORT_SYMBOL_GPL(rtas_error_rc);
  1323. int rtas_get_power_level(int powerdomain, int *level)
  1324. {
  1325. int token = rtas_function_token(RTAS_FN_GET_POWER_LEVEL);
  1326. int rc;
  1327. if (token == RTAS_UNKNOWN_SERVICE)
  1328. return -ENOENT;
  1329. while ((rc = rtas_call(token, 1, 2, level, powerdomain)) == RTAS_BUSY)
  1330. udelay(1);
  1331. if (rc < 0)
  1332. return rtas_error_rc(rc);
  1333. return rc;
  1334. }
  1335. EXPORT_SYMBOL_GPL(rtas_get_power_level);
  1336. int rtas_set_power_level(int powerdomain, int level, int *setlevel)
  1337. {
  1338. int token = rtas_function_token(RTAS_FN_SET_POWER_LEVEL);
  1339. int rc;
  1340. if (token == RTAS_UNKNOWN_SERVICE)
  1341. return -ENOENT;
  1342. do {
  1343. rc = rtas_call(token, 2, 2, setlevel, powerdomain, level);
  1344. } while (rtas_busy_delay(rc));
  1345. if (rc < 0)
  1346. return rtas_error_rc(rc);
  1347. return rc;
  1348. }
  1349. EXPORT_SYMBOL_GPL(rtas_set_power_level);
  1350. int rtas_get_sensor(int sensor, int index, int *state)
  1351. {
  1352. int token = rtas_function_token(RTAS_FN_GET_SENSOR_STATE);
  1353. int rc;
  1354. if (token == RTAS_UNKNOWN_SERVICE)
  1355. return -ENOENT;
  1356. do {
  1357. rc = rtas_call(token, 2, 2, state, sensor, index);
  1358. } while (rtas_busy_delay(rc));
  1359. if (rc < 0)
  1360. return rtas_error_rc(rc);
  1361. return rc;
  1362. }
  1363. EXPORT_SYMBOL_GPL(rtas_get_sensor);
  1364. int rtas_get_sensor_fast(int sensor, int index, int *state)
  1365. {
  1366. int token = rtas_function_token(RTAS_FN_GET_SENSOR_STATE);
  1367. int rc;
  1368. if (token == RTAS_UNKNOWN_SERVICE)
  1369. return -ENOENT;
  1370. rc = rtas_call(token, 2, 2, state, sensor, index);
  1371. WARN_ON(rc == RTAS_BUSY || (rc >= RTAS_EXTENDED_DELAY_MIN &&
  1372. rc <= RTAS_EXTENDED_DELAY_MAX));
  1373. if (rc < 0)
  1374. return rtas_error_rc(rc);
  1375. return rc;
  1376. }
  1377. bool rtas_indicator_present(int token, int *maxindex)
  1378. {
  1379. int proplen, count, i;
  1380. const struct indicator_elem {
  1381. __be32 token;
  1382. __be32 maxindex;
  1383. } *indicators;
  1384. indicators = of_get_property(rtas.dev, "rtas-indicators", &proplen);
  1385. if (!indicators)
  1386. return false;
  1387. count = proplen / sizeof(struct indicator_elem);
  1388. for (i = 0; i < count; i++) {
  1389. if (__be32_to_cpu(indicators[i].token) != token)
  1390. continue;
  1391. if (maxindex)
  1392. *maxindex = __be32_to_cpu(indicators[i].maxindex);
  1393. return true;
  1394. }
  1395. return false;
  1396. }
  1397. int rtas_set_indicator(int indicator, int index, int new_value)
  1398. {
  1399. int token = rtas_function_token(RTAS_FN_SET_INDICATOR);
  1400. int rc;
  1401. if (token == RTAS_UNKNOWN_SERVICE)
  1402. return -ENOENT;
  1403. do {
  1404. rc = rtas_call(token, 3, 1, NULL, indicator, index, new_value);
  1405. } while (rtas_busy_delay(rc));
  1406. if (rc < 0)
  1407. return rtas_error_rc(rc);
  1408. return rc;
  1409. }
  1410. EXPORT_SYMBOL_GPL(rtas_set_indicator);
  1411. /*
  1412. * Ignoring RTAS extended delay
  1413. */
  1414. int rtas_set_indicator_fast(int indicator, int index, int new_value)
  1415. {
  1416. int token = rtas_function_token(RTAS_FN_SET_INDICATOR);
  1417. int rc;
  1418. if (token == RTAS_UNKNOWN_SERVICE)
  1419. return -ENOENT;
  1420. rc = rtas_call(token, 3, 1, NULL, indicator, index, new_value);
  1421. WARN_ON(rc == RTAS_BUSY || (rc >= RTAS_EXTENDED_DELAY_MIN &&
  1422. rc <= RTAS_EXTENDED_DELAY_MAX));
  1423. if (rc < 0)
  1424. return rtas_error_rc(rc);
  1425. return rc;
  1426. }
  1427. /**
  1428. * rtas_ibm_suspend_me() - Call ibm,suspend-me to suspend the LPAR.
  1429. *
  1430. * @fw_status: RTAS call status will be placed here if not NULL.
  1431. *
  1432. * rtas_ibm_suspend_me() should be called only on a CPU which has
  1433. * received H_CONTINUE from the H_JOIN hcall. All other active CPUs
  1434. * should be waiting to return from H_JOIN.
  1435. *
  1436. * rtas_ibm_suspend_me() may suspend execution of the OS
  1437. * indefinitely. Callers should take appropriate measures upon return, such as
  1438. * resetting watchdog facilities.
  1439. *
  1440. * Callers may choose to retry this call if @fw_status is
  1441. * %RTAS_THREADS_ACTIVE.
  1442. *
  1443. * Return:
  1444. * 0 - The partition has resumed from suspend, possibly after
  1445. * migration to a different host.
  1446. * -ECANCELED - The operation was aborted.
  1447. * -EAGAIN - There were other CPUs not in H_JOIN at the time of the call.
  1448. * -EBUSY - Some other condition prevented the suspend from succeeding.
  1449. * -EIO - Hardware/platform error.
  1450. */
  1451. int rtas_ibm_suspend_me(int *fw_status)
  1452. {
  1453. int token = rtas_function_token(RTAS_FN_IBM_SUSPEND_ME);
  1454. int fwrc;
  1455. int ret;
  1456. fwrc = rtas_call(token, 0, 1, NULL);
  1457. switch (fwrc) {
  1458. case 0:
  1459. ret = 0;
  1460. break;
  1461. case RTAS_SUSPEND_ABORTED:
  1462. ret = -ECANCELED;
  1463. break;
  1464. case RTAS_THREADS_ACTIVE:
  1465. ret = -EAGAIN;
  1466. break;
  1467. case RTAS_NOT_SUSPENDABLE:
  1468. case RTAS_OUTSTANDING_COPROC:
  1469. ret = -EBUSY;
  1470. break;
  1471. case -1:
  1472. default:
  1473. ret = -EIO;
  1474. break;
  1475. }
  1476. if (fw_status)
  1477. *fw_status = fwrc;
  1478. return ret;
  1479. }
  1480. void __noreturn rtas_restart(char *cmd)
  1481. {
  1482. if (rtas_flash_term_hook)
  1483. rtas_flash_term_hook(SYS_RESTART);
  1484. pr_emerg("system-reboot returned %d\n",
  1485. rtas_call(rtas_function_token(RTAS_FN_SYSTEM_REBOOT), 0, 1, NULL));
  1486. for (;;);
  1487. }
  1488. void rtas_power_off(void)
  1489. {
  1490. if (rtas_flash_term_hook)
  1491. rtas_flash_term_hook(SYS_POWER_OFF);
  1492. /* allow power on only with power button press */
  1493. pr_emerg("power-off returned %d\n",
  1494. rtas_call(rtas_function_token(RTAS_FN_POWER_OFF), 2, 1, NULL, -1, -1));
  1495. for (;;);
  1496. }
  1497. void __noreturn rtas_halt(void)
  1498. {
  1499. if (rtas_flash_term_hook)
  1500. rtas_flash_term_hook(SYS_HALT);
  1501. /* allow power on only with power button press */
  1502. pr_emerg("power-off returned %d\n",
  1503. rtas_call(rtas_function_token(RTAS_FN_POWER_OFF), 2, 1, NULL, -1, -1));
  1504. for (;;);
  1505. }
  1506. /* Must be in the RMO region, so we place it here */
  1507. static char rtas_os_term_buf[2048];
  1508. static bool ibm_extended_os_term;
  1509. void rtas_os_term(char *str)
  1510. {
  1511. s32 token = rtas_function_token(RTAS_FN_IBM_OS_TERM);
  1512. static struct rtas_args args;
  1513. int status;
  1514. /*
  1515. * Firmware with the ibm,extended-os-term property is guaranteed
  1516. * to always return from an ibm,os-term call. Earlier versions without
  1517. * this property may terminate the partition which we want to avoid
  1518. * since it interferes with panic_timeout.
  1519. */
  1520. if (token == RTAS_UNKNOWN_SERVICE || !ibm_extended_os_term)
  1521. return;
  1522. snprintf(rtas_os_term_buf, 2048, "OS panic: %s", str);
  1523. /*
  1524. * Keep calling as long as RTAS returns a "try again" status,
  1525. * but don't use rtas_busy_delay(), which potentially
  1526. * schedules.
  1527. */
  1528. do {
  1529. rtas_call_unlocked(&args, token, 1, 1, NULL, __pa(rtas_os_term_buf));
  1530. status = be32_to_cpu(args.rets[0]);
  1531. } while (rtas_busy_delay_time(status));
  1532. if (status != 0)
  1533. pr_emerg("ibm,os-term call failed %d\n", status);
  1534. }
  1535. /**
  1536. * rtas_activate_firmware() - Activate a new version of firmware.
  1537. *
  1538. * Context: This function may sleep.
  1539. *
  1540. * Activate a new version of partition firmware. The OS must call this
  1541. * after resuming from a partition hibernation or migration in order
  1542. * to maintain the ability to perform live firmware updates. It's not
  1543. * catastrophic for this method to be absent or to fail; just log the
  1544. * condition in that case.
  1545. */
  1546. void rtas_activate_firmware(void)
  1547. {
  1548. int token = rtas_function_token(RTAS_FN_IBM_ACTIVATE_FIRMWARE);
  1549. int fwrc;
  1550. if (token == RTAS_UNKNOWN_SERVICE) {
  1551. pr_notice("ibm,activate-firmware method unavailable\n");
  1552. return;
  1553. }
  1554. mutex_lock(&rtas_ibm_activate_firmware_lock);
  1555. do {
  1556. fwrc = rtas_call(token, 0, 1, NULL);
  1557. } while (rtas_busy_delay(fwrc));
  1558. mutex_unlock(&rtas_ibm_activate_firmware_lock);
  1559. if (fwrc)
  1560. pr_err("ibm,activate-firmware failed (%i)\n", fwrc);
  1561. }
  1562. /**
  1563. * get_pseries_errorlog() - Find a specific pseries error log in an RTAS
  1564. * extended event log.
  1565. * @log: RTAS error/event log
  1566. * @section_id: two character section identifier
  1567. *
  1568. * Return: A pointer to the specified errorlog or NULL if not found.
  1569. */
  1570. noinstr struct pseries_errorlog *get_pseries_errorlog(struct rtas_error_log *log,
  1571. uint16_t section_id)
  1572. {
  1573. struct rtas_ext_event_log_v6 *ext_log =
  1574. (struct rtas_ext_event_log_v6 *)log->buffer;
  1575. struct pseries_errorlog *sect;
  1576. unsigned char *p, *log_end;
  1577. uint32_t ext_log_length = rtas_error_extended_log_length(log);
  1578. uint8_t log_format = rtas_ext_event_log_format(ext_log);
  1579. uint32_t company_id = rtas_ext_event_company_id(ext_log);
  1580. /* Check that we understand the format */
  1581. if (ext_log_length < sizeof(struct rtas_ext_event_log_v6) ||
  1582. log_format != RTAS_V6EXT_LOG_FORMAT_EVENT_LOG ||
  1583. company_id != RTAS_V6EXT_COMPANY_ID_IBM)
  1584. return NULL;
  1585. log_end = log->buffer + ext_log_length;
  1586. p = ext_log->vendor_log;
  1587. while (p < log_end) {
  1588. sect = (struct pseries_errorlog *)p;
  1589. if (pseries_errorlog_id(sect) == section_id)
  1590. return sect;
  1591. p += pseries_errorlog_length(sect);
  1592. }
  1593. return NULL;
  1594. }
  1595. /*
  1596. * The sys_rtas syscall, as originally designed, allows root to pass
  1597. * arbitrary physical addresses to RTAS calls. A number of RTAS calls
  1598. * can be abused to write to arbitrary memory and do other things that
  1599. * are potentially harmful to system integrity, and thus should only
  1600. * be used inside the kernel and not exposed to userspace.
  1601. *
  1602. * All known legitimate users of the sys_rtas syscall will only ever
  1603. * pass addresses that fall within the RMO buffer, and use a known
  1604. * subset of RTAS calls.
  1605. *
  1606. * Accordingly, we filter RTAS requests to check that the call is
  1607. * permitted, and that provided pointers fall within the RMO buffer.
  1608. * If a function is allowed to be invoked via the syscall, then its
  1609. * entry in the rtas_functions table points to a rtas_filter that
  1610. * describes its constraints, with the indexes of the parameters which
  1611. * are expected to contain addresses and sizes of buffers allocated
  1612. * inside the RMO buffer.
  1613. */
  1614. static bool in_rmo_buf(u32 base, u32 end)
  1615. {
  1616. return base >= rtas_rmo_buf &&
  1617. base < (rtas_rmo_buf + RTAS_USER_REGION_SIZE) &&
  1618. base <= end &&
  1619. end >= rtas_rmo_buf &&
  1620. end < (rtas_rmo_buf + RTAS_USER_REGION_SIZE);
  1621. }
  1622. static bool block_rtas_call(const struct rtas_function *func, int nargs,
  1623. struct rtas_args *args)
  1624. {
  1625. const struct rtas_filter *f;
  1626. const bool is_platform_dump =
  1627. func == &rtas_function_table[RTAS_FNIDX__IBM_PLATFORM_DUMP];
  1628. const bool is_config_conn =
  1629. func == &rtas_function_table[RTAS_FNIDX__IBM_CONFIGURE_CONNECTOR];
  1630. u32 base, size, end;
  1631. /*
  1632. * Only functions with filters attached are allowed.
  1633. */
  1634. f = func->filter;
  1635. if (!f)
  1636. goto err;
  1637. /*
  1638. * And some functions aren't allowed on LE.
  1639. */
  1640. if (IS_ENABLED(CONFIG_CPU_LITTLE_ENDIAN) && func->banned_for_syscall_on_le)
  1641. goto err;
  1642. if (f->buf_idx1 != -1) {
  1643. base = be32_to_cpu(args->args[f->buf_idx1]);
  1644. if (f->size_idx1 != -1)
  1645. size = be32_to_cpu(args->args[f->size_idx1]);
  1646. else if (f->fixed_size)
  1647. size = f->fixed_size;
  1648. else
  1649. size = 1;
  1650. end = base + size - 1;
  1651. /*
  1652. * Special case for ibm,platform-dump - NULL buffer
  1653. * address is used to indicate end of dump processing
  1654. */
  1655. if (is_platform_dump && base == 0)
  1656. return false;
  1657. if (!in_rmo_buf(base, end))
  1658. goto err;
  1659. }
  1660. if (f->buf_idx2 != -1) {
  1661. base = be32_to_cpu(args->args[f->buf_idx2]);
  1662. if (f->size_idx2 != -1)
  1663. size = be32_to_cpu(args->args[f->size_idx2]);
  1664. else if (f->fixed_size)
  1665. size = f->fixed_size;
  1666. else
  1667. size = 1;
  1668. end = base + size - 1;
  1669. /*
  1670. * Special case for ibm,configure-connector where the
  1671. * address can be 0
  1672. */
  1673. if (is_config_conn && base == 0)
  1674. return false;
  1675. if (!in_rmo_buf(base, end))
  1676. goto err;
  1677. }
  1678. return false;
  1679. err:
  1680. pr_err_ratelimited("sys_rtas: RTAS call blocked - exploit attempt?\n");
  1681. pr_err_ratelimited("sys_rtas: %s nargs=%d (called by %s)\n",
  1682. func->name, nargs, current->comm);
  1683. return true;
  1684. }
  1685. /* We assume to be passed big endian arguments */
  1686. SYSCALL_DEFINE1(rtas, struct rtas_args __user *, uargs)
  1687. {
  1688. const struct rtas_function *func;
  1689. struct pin_cookie cookie;
  1690. struct rtas_args args;
  1691. unsigned long flags;
  1692. char *buff_copy, *errbuf = NULL;
  1693. int nargs, nret, token;
  1694. if (!capable(CAP_SYS_ADMIN))
  1695. return -EPERM;
  1696. if (!rtas.entry)
  1697. return -EINVAL;
  1698. if (copy_from_user(&args, uargs, 3 * sizeof(u32)) != 0)
  1699. return -EFAULT;
  1700. nargs = be32_to_cpu(args.nargs);
  1701. nret = be32_to_cpu(args.nret);
  1702. token = be32_to_cpu(args.token);
  1703. if (nargs >= ARRAY_SIZE(args.args)
  1704. || nret > ARRAY_SIZE(args.args)
  1705. || nargs + nret > ARRAY_SIZE(args.args))
  1706. return -EINVAL;
  1707. nargs = array_index_nospec(nargs, ARRAY_SIZE(args.args));
  1708. nret = array_index_nospec(nret, ARRAY_SIZE(args.args) - nargs);
  1709. /* Copy in args. */
  1710. if (copy_from_user(args.args, uargs->args,
  1711. nargs * sizeof(rtas_arg_t)) != 0)
  1712. return -EFAULT;
  1713. /*
  1714. * If this token doesn't correspond to a function the kernel
  1715. * understands, you're not allowed to call it.
  1716. */
  1717. func = rtas_token_to_function_untrusted(token);
  1718. if (!func)
  1719. return -EINVAL;
  1720. args.rets = &args.args[nargs];
  1721. memset(args.rets, 0, nret * sizeof(rtas_arg_t));
  1722. if (block_rtas_call(func, nargs, &args))
  1723. return -EINVAL;
  1724. if (token_is_restricted_errinjct(token)) {
  1725. int err;
  1726. err = security_locked_down(LOCKDOWN_RTAS_ERROR_INJECTION);
  1727. if (err)
  1728. return err;
  1729. }
  1730. /* Need to handle ibm,suspend_me call specially */
  1731. if (token == rtas_function_token(RTAS_FN_IBM_SUSPEND_ME)) {
  1732. /*
  1733. * rtas_ibm_suspend_me assumes the streamid handle is in cpu
  1734. * endian, or at least the hcall within it requires it.
  1735. */
  1736. int rc = 0;
  1737. u64 handle = ((u64)be32_to_cpu(args.args[0]) << 32)
  1738. | be32_to_cpu(args.args[1]);
  1739. rc = rtas_syscall_dispatch_ibm_suspend_me(handle);
  1740. if (rc == -EAGAIN)
  1741. args.rets[0] = cpu_to_be32(RTAS_NOT_SUSPENDABLE);
  1742. else if (rc == -EIO)
  1743. args.rets[0] = cpu_to_be32(-1);
  1744. else if (rc)
  1745. return rc;
  1746. goto copy_return;
  1747. }
  1748. buff_copy = get_errorlog_buffer();
  1749. /*
  1750. * If this function has a mutex assigned to it, we must
  1751. * acquire it to avoid interleaving with any kernel-based uses
  1752. * of the same function. Kernel-based sequences acquire the
  1753. * appropriate mutex explicitly.
  1754. */
  1755. if (func->lock)
  1756. mutex_lock(func->lock);
  1757. raw_spin_lock_irqsave(&rtas_lock, flags);
  1758. cookie = lockdep_pin_lock(&rtas_lock);
  1759. rtas_args = args;
  1760. do_enter_rtas(&rtas_args);
  1761. args = rtas_args;
  1762. /* A -1 return code indicates that the last command couldn't
  1763. be completed due to a hardware error. */
  1764. if (be32_to_cpu(args.rets[0]) == -1)
  1765. errbuf = __fetch_rtas_last_error(buff_copy);
  1766. lockdep_unpin_lock(&rtas_lock, cookie);
  1767. raw_spin_unlock_irqrestore(&rtas_lock, flags);
  1768. if (func->lock)
  1769. mutex_unlock(func->lock);
  1770. if (buff_copy) {
  1771. if (errbuf)
  1772. log_error(errbuf, ERR_TYPE_RTAS_LOG, 0);
  1773. kfree(buff_copy);
  1774. }
  1775. copy_return:
  1776. /* Copy out args. */
  1777. if (copy_to_user(uargs->args + nargs,
  1778. args.args + nargs,
  1779. nret * sizeof(rtas_arg_t)) != 0)
  1780. return -EFAULT;
  1781. return 0;
  1782. }
  1783. static void __init rtas_function_table_init(void)
  1784. {
  1785. struct property *prop;
  1786. for (size_t i = 0; i < ARRAY_SIZE(rtas_function_table); ++i) {
  1787. struct rtas_function *curr = &rtas_function_table[i];
  1788. struct rtas_function *prior;
  1789. int cmp;
  1790. curr->token = RTAS_UNKNOWN_SERVICE;
  1791. if (i == 0)
  1792. continue;
  1793. /*
  1794. * Ensure table is sorted correctly for binary search
  1795. * on function names.
  1796. */
  1797. prior = &rtas_function_table[i - 1];
  1798. cmp = strcmp(prior->name, curr->name);
  1799. if (cmp < 0)
  1800. continue;
  1801. if (cmp == 0) {
  1802. pr_err("'%s' has duplicate function table entries\n",
  1803. curr->name);
  1804. } else {
  1805. pr_err("function table unsorted: '%s' wrongly precedes '%s'\n",
  1806. prior->name, curr->name);
  1807. }
  1808. }
  1809. for_each_property_of_node(rtas.dev, prop) {
  1810. struct rtas_function *func;
  1811. if (prop->length != sizeof(u32))
  1812. continue;
  1813. func = __rtas_name_to_function(prop->name);
  1814. if (!func)
  1815. continue;
  1816. func->token = be32_to_cpup((__be32 *)prop->value);
  1817. pr_debug("function %s has token %u\n", func->name, func->token);
  1818. }
  1819. }
  1820. /*
  1821. * Call early during boot, before mem init, to retrieve the RTAS
  1822. * information from the device-tree and allocate the RMO buffer for userland
  1823. * accesses.
  1824. */
  1825. void __init rtas_initialize(void)
  1826. {
  1827. unsigned long rtas_region = RTAS_INSTANTIATE_MAX;
  1828. u32 base, size, entry;
  1829. int no_base, no_size, no_entry;
  1830. /* Get RTAS dev node and fill up our "rtas" structure with infos
  1831. * about it.
  1832. */
  1833. rtas.dev = of_find_node_by_name(NULL, "rtas");
  1834. if (!rtas.dev)
  1835. return;
  1836. no_base = of_property_read_u32(rtas.dev, "linux,rtas-base", &base);
  1837. no_size = of_property_read_u32(rtas.dev, "rtas-size", &size);
  1838. if (no_base || no_size) {
  1839. of_node_put(rtas.dev);
  1840. rtas.dev = NULL;
  1841. return;
  1842. }
  1843. rtas.base = base;
  1844. rtas.size = size;
  1845. no_entry = of_property_read_u32(rtas.dev, "linux,rtas-entry", &entry);
  1846. rtas.entry = no_entry ? rtas.base : entry;
  1847. init_error_log_max();
  1848. /* Must be called before any function token lookups */
  1849. rtas_function_table_init();
  1850. /*
  1851. * Discover this now to avoid a device tree lookup in the
  1852. * panic path.
  1853. */
  1854. ibm_extended_os_term = of_property_read_bool(rtas.dev, "ibm,extended-os-term");
  1855. /* If RTAS was found, allocate the RMO buffer for it and look for
  1856. * the stop-self token if any
  1857. */
  1858. #ifdef CONFIG_PPC64
  1859. if (firmware_has_feature(FW_FEATURE_LPAR))
  1860. rtas_region = min(ppc64_rma_size, RTAS_INSTANTIATE_MAX);
  1861. #endif
  1862. rtas_rmo_buf = memblock_phys_alloc_range(RTAS_USER_REGION_SIZE, PAGE_SIZE,
  1863. 0, rtas_region);
  1864. if (!rtas_rmo_buf)
  1865. panic("ERROR: RTAS: Failed to allocate %lx bytes below %pa\n",
  1866. PAGE_SIZE, &rtas_region);
  1867. rtas_work_area_reserve_arena(rtas_region);
  1868. }
  1869. int __init early_init_dt_scan_rtas(unsigned long node,
  1870. const char *uname, int depth, void *data)
  1871. {
  1872. const u32 *basep, *entryp, *sizep;
  1873. if (depth != 1 || strcmp(uname, "rtas") != 0)
  1874. return 0;
  1875. basep = of_get_flat_dt_prop(node, "linux,rtas-base", NULL);
  1876. entryp = of_get_flat_dt_prop(node, "linux,rtas-entry", NULL);
  1877. sizep = of_get_flat_dt_prop(node, "rtas-size", NULL);
  1878. #ifdef CONFIG_PPC64
  1879. /* need this feature to decide the crashkernel offset */
  1880. if (of_get_flat_dt_prop(node, "ibm,hypertas-functions", NULL))
  1881. powerpc_firmware_features |= FW_FEATURE_LPAR;
  1882. #endif
  1883. if (basep && entryp && sizep) {
  1884. rtas.base = *basep;
  1885. rtas.entry = *entryp;
  1886. rtas.size = *sizep;
  1887. }
  1888. #ifdef CONFIG_UDBG_RTAS_CONSOLE
  1889. basep = of_get_flat_dt_prop(node, "put-term-char", NULL);
  1890. if (basep)
  1891. rtas_putchar_token = *basep;
  1892. basep = of_get_flat_dt_prop(node, "get-term-char", NULL);
  1893. if (basep)
  1894. rtas_getchar_token = *basep;
  1895. if (rtas_putchar_token != RTAS_UNKNOWN_SERVICE &&
  1896. rtas_getchar_token != RTAS_UNKNOWN_SERVICE)
  1897. udbg_init_rtas_console();
  1898. #endif
  1899. /* break now */
  1900. return 1;
  1901. }
  1902. static DEFINE_RAW_SPINLOCK(timebase_lock);
  1903. static u64 timebase = 0;
  1904. void rtas_give_timebase(void)
  1905. {
  1906. unsigned long flags;
  1907. raw_spin_lock_irqsave(&timebase_lock, flags);
  1908. hard_irq_disable();
  1909. rtas_call(rtas_function_token(RTAS_FN_FREEZE_TIME_BASE), 0, 1, NULL);
  1910. timebase = get_tb();
  1911. raw_spin_unlock(&timebase_lock);
  1912. while (timebase)
  1913. barrier();
  1914. rtas_call(rtas_function_token(RTAS_FN_THAW_TIME_BASE), 0, 1, NULL);
  1915. local_irq_restore(flags);
  1916. }
  1917. void rtas_take_timebase(void)
  1918. {
  1919. while (!timebase)
  1920. barrier();
  1921. raw_spin_lock(&timebase_lock);
  1922. set_tb(timebase >> 32, timebase & 0xffffffff);
  1923. timebase = 0;
  1924. raw_spin_unlock(&timebase_lock);
  1925. }