mpam.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (C) 2025 Arm Ltd.
  3. /* Parse the MPAM ACPI table feeding the discovered nodes into the driver */
  4. #define pr_fmt(fmt) "ACPI MPAM: " fmt
  5. #include <linux/acpi.h>
  6. #include <linux/arm_mpam.h>
  7. #include <linux/bits.h>
  8. #include <linux/cpu.h>
  9. #include <linux/cpumask.h>
  10. #include <linux/platform_device.h>
  11. #include <acpi/processor.h>
  12. /*
  13. * Flags for acpi_table_mpam_msc.*_interrupt_flags.
  14. * See 2.1.1 Interrupt Flags, Table 5, of DEN0065B_MPAM_ACPI_3.0-bet.
  15. */
  16. #define ACPI_MPAM_MSC_IRQ_MODE BIT(0)
  17. #define ACPI_MPAM_MSC_IRQ_TYPE_MASK GENMASK(2, 1)
  18. #define ACPI_MPAM_MSC_IRQ_TYPE_WIRED 0
  19. #define ACPI_MPAM_MSC_IRQ_AFFINITY_TYPE_MASK BIT(3)
  20. #define ACPI_MPAM_MSC_IRQ_AFFINITY_TYPE_PROCESSOR 0
  21. #define ACPI_MPAM_MSC_IRQ_AFFINITY_TYPE_PROCESSOR_CONTAINER 1
  22. #define ACPI_MPAM_MSC_IRQ_AFFINITY_VALID BIT(4)
  23. /*
  24. * Encodings for the MSC node body interface type field.
  25. * See 2.1 MPAM MSC node, Table 4 of DEN0065B_MPAM_ACPI_3.0-bet.
  26. */
  27. #define ACPI_MPAM_MSC_IFACE_MMIO 0x00
  28. #define ACPI_MPAM_MSC_IFACE_PCC 0x0a
  29. static bool _is_ppi_partition(u32 flags)
  30. {
  31. u32 aff_type, is_ppi;
  32. bool ret;
  33. is_ppi = FIELD_GET(ACPI_MPAM_MSC_IRQ_AFFINITY_VALID, flags);
  34. if (!is_ppi)
  35. return false;
  36. aff_type = FIELD_GET(ACPI_MPAM_MSC_IRQ_AFFINITY_TYPE_MASK, flags);
  37. ret = (aff_type == ACPI_MPAM_MSC_IRQ_AFFINITY_TYPE_PROCESSOR_CONTAINER);
  38. if (ret)
  39. pr_err_once("Partitioned interrupts not supported\n");
  40. return ret;
  41. }
  42. static int acpi_mpam_register_irq(struct platform_device *pdev,
  43. u32 intid, u32 flags)
  44. {
  45. int irq;
  46. u32 int_type;
  47. int trigger;
  48. if (!intid)
  49. return -EINVAL;
  50. if (_is_ppi_partition(flags))
  51. return -EINVAL;
  52. trigger = FIELD_GET(ACPI_MPAM_MSC_IRQ_MODE, flags);
  53. int_type = FIELD_GET(ACPI_MPAM_MSC_IRQ_TYPE_MASK, flags);
  54. if (int_type != ACPI_MPAM_MSC_IRQ_TYPE_WIRED)
  55. return -EINVAL;
  56. irq = acpi_register_gsi(&pdev->dev, intid, trigger, ACPI_ACTIVE_HIGH);
  57. if (irq < 0)
  58. pr_err_once("Failed to register interrupt 0x%x with ACPI\n", intid);
  59. return irq;
  60. }
  61. static void acpi_mpam_parse_irqs(struct platform_device *pdev,
  62. struct acpi_mpam_msc_node *tbl_msc,
  63. struct resource *res, int *res_idx)
  64. {
  65. u32 flags, intid;
  66. int irq;
  67. intid = tbl_msc->overflow_interrupt;
  68. flags = tbl_msc->overflow_interrupt_flags;
  69. irq = acpi_mpam_register_irq(pdev, intid, flags);
  70. if (irq > 0)
  71. res[(*res_idx)++] = DEFINE_RES_IRQ_NAMED(irq, "overflow");
  72. intid = tbl_msc->error_interrupt;
  73. flags = tbl_msc->error_interrupt_flags;
  74. irq = acpi_mpam_register_irq(pdev, intid, flags);
  75. if (irq > 0)
  76. res[(*res_idx)++] = DEFINE_RES_IRQ_NAMED(irq, "error");
  77. }
  78. static int acpi_mpam_parse_resource(struct mpam_msc *msc,
  79. struct acpi_mpam_resource_node *res)
  80. {
  81. int level, nid;
  82. u32 cache_id;
  83. switch (res->locator_type) {
  84. case ACPI_MPAM_LOCATION_TYPE_PROCESSOR_CACHE:
  85. cache_id = res->locator.cache_locator.cache_reference;
  86. level = find_acpi_cache_level_from_id(cache_id);
  87. if (level <= 0) {
  88. pr_err_once("Bad level (%d) for cache with id %u\n", level, cache_id);
  89. return -EINVAL;
  90. }
  91. return mpam_ris_create(msc, res->ris_index, MPAM_CLASS_CACHE,
  92. level, cache_id);
  93. case ACPI_MPAM_LOCATION_TYPE_MEMORY:
  94. nid = pxm_to_node(res->locator.memory_locator.proximity_domain);
  95. if (nid == NUMA_NO_NODE) {
  96. pr_debug("Bad proximity domain %lld, using node 0 instead\n",
  97. res->locator.memory_locator.proximity_domain);
  98. nid = 0;
  99. }
  100. return mpam_ris_create(msc, res->ris_index, MPAM_CLASS_MEMORY,
  101. MPAM_CLASS_ID_DEFAULT, nid);
  102. default:
  103. /* These get discovered later and are treated as unknown */
  104. return 0;
  105. }
  106. }
  107. int acpi_mpam_parse_resources(struct mpam_msc *msc,
  108. struct acpi_mpam_msc_node *tbl_msc)
  109. {
  110. int i, err;
  111. char *ptr, *table_end;
  112. struct acpi_mpam_resource_node *resource;
  113. table_end = (char *)tbl_msc + tbl_msc->length;
  114. ptr = (char *)(tbl_msc + 1);
  115. for (i = 0; i < tbl_msc->num_resource_nodes; i++) {
  116. u64 max_deps, remaining_table;
  117. if (ptr + sizeof(*resource) > table_end)
  118. return -EINVAL;
  119. resource = (struct acpi_mpam_resource_node *)ptr;
  120. remaining_table = table_end - ptr;
  121. max_deps = remaining_table / sizeof(struct acpi_mpam_func_deps);
  122. if (resource->num_functional_deps > max_deps) {
  123. pr_debug("MSC has impossible number of functional dependencies\n");
  124. return -EINVAL;
  125. }
  126. err = acpi_mpam_parse_resource(msc, resource);
  127. if (err)
  128. return err;
  129. ptr += sizeof(*resource);
  130. ptr += resource->num_functional_deps * sizeof(struct acpi_mpam_func_deps);
  131. }
  132. return 0;
  133. }
  134. /*
  135. * Creates the device power management link and returns true if the
  136. * acpi id is valid and usable for cpu affinity. This is the case
  137. * when the linked device is a processor or a processor container.
  138. */
  139. static bool __init parse_msc_pm_link(struct acpi_mpam_msc_node *tbl_msc,
  140. struct platform_device *pdev,
  141. u32 *acpi_id)
  142. {
  143. char hid[sizeof(tbl_msc->hardware_id_linked_device) + 1] = { 0 };
  144. bool acpi_id_valid = false;
  145. struct acpi_device *buddy;
  146. char uid[11];
  147. int len;
  148. memcpy(hid, &tbl_msc->hardware_id_linked_device,
  149. sizeof(tbl_msc->hardware_id_linked_device));
  150. if (!strcmp(hid, ACPI_PROCESSOR_CONTAINER_HID)) {
  151. *acpi_id = tbl_msc->instance_id_linked_device;
  152. acpi_id_valid = true;
  153. }
  154. len = snprintf(uid, sizeof(uid), "%u",
  155. tbl_msc->instance_id_linked_device);
  156. if (len >= sizeof(uid)) {
  157. pr_debug("Failed to convert uid of device for power management.");
  158. return acpi_id_valid;
  159. }
  160. buddy = acpi_dev_get_first_match_dev(hid, uid, -1);
  161. if (buddy) {
  162. device_link_add(&pdev->dev, &buddy->dev, DL_FLAG_STATELESS);
  163. acpi_dev_put(buddy);
  164. }
  165. return acpi_id_valid;
  166. }
  167. static int decode_interface_type(struct acpi_mpam_msc_node *tbl_msc,
  168. enum mpam_msc_iface *iface)
  169. {
  170. switch (tbl_msc->interface_type) {
  171. case ACPI_MPAM_MSC_IFACE_MMIO:
  172. *iface = MPAM_IFACE_MMIO;
  173. return 0;
  174. case ACPI_MPAM_MSC_IFACE_PCC:
  175. *iface = MPAM_IFACE_PCC;
  176. return 0;
  177. default:
  178. return -EINVAL;
  179. }
  180. }
  181. static struct platform_device * __init acpi_mpam_parse_msc(struct acpi_mpam_msc_node *tbl_msc)
  182. {
  183. struct platform_device *pdev __free(platform_device_put) =
  184. platform_device_alloc("mpam_msc", tbl_msc->identifier);
  185. int next_res = 0, next_prop = 0, err;
  186. /* pcc, nrdy, affinity and a sentinel */
  187. struct property_entry props[4] = { 0 };
  188. /* mmio, 2xirq, no sentinel. */
  189. struct resource res[3] = { 0 };
  190. struct acpi_device *companion;
  191. enum mpam_msc_iface iface;
  192. char uid[16];
  193. u32 acpi_id;
  194. if (!pdev)
  195. return ERR_PTR(-ENOMEM);
  196. /* Some power management is described in the namespace: */
  197. err = snprintf(uid, sizeof(uid), "%u", tbl_msc->identifier);
  198. if (err > 0 && err < sizeof(uid)) {
  199. companion = acpi_dev_get_first_match_dev("ARMHAA5C", uid, -1);
  200. if (companion) {
  201. ACPI_COMPANION_SET(&pdev->dev, companion);
  202. acpi_dev_put(companion);
  203. } else {
  204. pr_debug("MSC.%u: missing namespace entry\n", tbl_msc->identifier);
  205. }
  206. }
  207. if (decode_interface_type(tbl_msc, &iface)) {
  208. pr_debug("MSC.%u: unknown interface type\n", tbl_msc->identifier);
  209. return ERR_PTR(-EINVAL);
  210. }
  211. if (iface == MPAM_IFACE_MMIO) {
  212. res[next_res++] = DEFINE_RES_MEM_NAMED(tbl_msc->base_address,
  213. tbl_msc->mmio_size,
  214. "MPAM:MSC");
  215. } else if (iface == MPAM_IFACE_PCC) {
  216. props[next_prop++] = PROPERTY_ENTRY_U32("pcc-channel",
  217. tbl_msc->base_address);
  218. }
  219. acpi_mpam_parse_irqs(pdev, tbl_msc, res, &next_res);
  220. WARN_ON_ONCE(next_res > ARRAY_SIZE(res));
  221. err = platform_device_add_resources(pdev, res, next_res);
  222. if (err)
  223. return ERR_PTR(err);
  224. props[next_prop++] = PROPERTY_ENTRY_U32("arm,not-ready-us",
  225. tbl_msc->max_nrdy_usec);
  226. /*
  227. * The MSC's CPU affinity is described via its linked power
  228. * management device, but only if it points at a Processor or
  229. * Processor Container.
  230. */
  231. if (parse_msc_pm_link(tbl_msc, pdev, &acpi_id))
  232. props[next_prop++] = PROPERTY_ENTRY_U32("cpu_affinity", acpi_id);
  233. WARN_ON_ONCE(next_prop > ARRAY_SIZE(props) - 1);
  234. err = device_create_managed_software_node(&pdev->dev, props, NULL);
  235. if (err)
  236. return ERR_PTR(err);
  237. /*
  238. * Stash the table entry for acpi_mpam_parse_resources() to discover
  239. * what this MSC controls.
  240. */
  241. err = platform_device_add_data(pdev, tbl_msc, tbl_msc->length);
  242. if (err)
  243. return ERR_PTR(err);
  244. err = platform_device_add(pdev);
  245. if (err)
  246. return ERR_PTR(err);
  247. return_ptr(pdev);
  248. }
  249. static int __init acpi_mpam_parse(void)
  250. {
  251. char *table_end, *table_offset;
  252. struct acpi_mpam_msc_node *tbl_msc;
  253. struct platform_device *pdev;
  254. if (acpi_disabled || !system_supports_mpam())
  255. return 0;
  256. struct acpi_table_header *table __free(acpi_put_table) =
  257. acpi_get_table_pointer(ACPI_SIG_MPAM, 0);
  258. if (IS_ERR(table))
  259. return 0;
  260. if (table->revision < 1) {
  261. pr_debug("MPAM ACPI table revision %d not supported\n", table->revision);
  262. return 0;
  263. }
  264. table_offset = (char *)(table + 1);
  265. table_end = (char *)table + table->length;
  266. while (table_offset < table_end) {
  267. tbl_msc = (struct acpi_mpam_msc_node *)table_offset;
  268. if (table_offset + sizeof(*tbl_msc) > table_end ||
  269. table_offset + tbl_msc->length > table_end) {
  270. pr_err("MSC entry overlaps end of ACPI table\n");
  271. return -EINVAL;
  272. }
  273. table_offset += tbl_msc->length;
  274. /*
  275. * If any of the reserved fields are set, make no attempt to
  276. * parse the MSC structure. This MSC will still be counted by
  277. * acpi_mpam_count_msc(), meaning the MPAM driver can't probe
  278. * against all MSC, and will never be enabled. There is no way
  279. * to enable it safely, because we cannot determine safe
  280. * system-wide partid and pmg ranges in this situation.
  281. */
  282. if (tbl_msc->reserved || tbl_msc->reserved1 || tbl_msc->reserved2) {
  283. pr_err_once("Unrecognised MSC, MPAM not usable\n");
  284. pr_debug("MSC.%u: reserved field set\n", tbl_msc->identifier);
  285. continue;
  286. }
  287. if (!tbl_msc->mmio_size) {
  288. pr_debug("MSC.%u: marked as disabled\n", tbl_msc->identifier);
  289. continue;
  290. }
  291. pdev = acpi_mpam_parse_msc(tbl_msc);
  292. if (IS_ERR(pdev))
  293. return PTR_ERR(pdev);
  294. }
  295. return 0;
  296. }
  297. /**
  298. * acpi_mpam_count_msc() - Count the number of MSC described by firmware.
  299. *
  300. * Returns the number of MSCs, or zero for an error.
  301. *
  302. * This can be called before or in parallel with acpi_mpam_parse().
  303. */
  304. int acpi_mpam_count_msc(void)
  305. {
  306. char *table_end, *table_offset;
  307. struct acpi_mpam_msc_node *tbl_msc;
  308. int count = 0;
  309. if (acpi_disabled || !system_supports_mpam())
  310. return 0;
  311. struct acpi_table_header *table __free(acpi_put_table) =
  312. acpi_get_table_pointer(ACPI_SIG_MPAM, 0);
  313. if (IS_ERR(table))
  314. return 0;
  315. if (table->revision < 1)
  316. return 0;
  317. table_offset = (char *)(table + 1);
  318. table_end = (char *)table + table->length;
  319. while (table_offset < table_end) {
  320. tbl_msc = (struct acpi_mpam_msc_node *)table_offset;
  321. if (table_offset + sizeof(*tbl_msc) > table_end)
  322. return -EINVAL;
  323. if (tbl_msc->length < sizeof(*tbl_msc))
  324. return -EINVAL;
  325. if (tbl_msc->length > table_end - table_offset)
  326. return -EINVAL;
  327. table_offset += tbl_msc->length;
  328. if (!tbl_msc->mmio_size)
  329. continue;
  330. count++;
  331. }
  332. return count;
  333. }
  334. /*
  335. * Call after ACPI devices have been created, which happens behind acpi_scan_init()
  336. * called from subsys_initcall(). PCC requires the mailbox driver, which is
  337. * initialised from postcore_initcall().
  338. */
  339. subsys_initcall_sync(acpi_mpam_parse);