opal-imc.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * OPAL IMC interface detection driver
  4. * Supported on POWERNV platform
  5. *
  6. * Copyright (C) 2017 Madhavan Srinivasan, IBM Corporation.
  7. * (C) 2017 Anju T Sudhakar, IBM Corporation.
  8. * (C) 2017 Hemant K Shaw, IBM Corporation.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/of.h>
  13. #include <linux/of_address.h>
  14. #include <linux/crash_dump.h>
  15. #include <linux/debugfs.h>
  16. #include <asm/opal.h>
  17. #include <asm/io.h>
  18. #include <asm/imc-pmu.h>
  19. #include <asm/cputhreads.h>
  20. static struct dentry *imc_debugfs_parent;
  21. /* Helpers to export imc command and mode via debugfs */
  22. static int imc_mem_get(void *data, u64 *val)
  23. {
  24. *val = cpu_to_be64(*(u64 *)data);
  25. return 0;
  26. }
  27. static int imc_mem_set(void *data, u64 val)
  28. {
  29. *(u64 *)data = cpu_to_be64(val);
  30. return 0;
  31. }
  32. DEFINE_DEBUGFS_ATTRIBUTE(fops_imc_x64, imc_mem_get, imc_mem_set, "0x%016llx\n");
  33. static void imc_debugfs_create_x64(const char *name, umode_t mode,
  34. struct dentry *parent, u64 *value)
  35. {
  36. debugfs_create_file_unsafe(name, mode, parent, value, &fops_imc_x64);
  37. }
  38. /*
  39. * export_imc_mode_and_cmd: Create a debugfs interface
  40. * for imc_cmd and imc_mode
  41. * for each node in the system.
  42. * imc_mode and imc_cmd can be changed by echo into
  43. * this interface.
  44. */
  45. static void export_imc_mode_and_cmd(struct device_node *node,
  46. struct imc_pmu *pmu_ptr)
  47. {
  48. static u64 loc, *imc_mode_addr, *imc_cmd_addr;
  49. char mode[16], cmd[16];
  50. u32 cb_offset;
  51. struct imc_mem_info *ptr = pmu_ptr->mem_info;
  52. imc_debugfs_parent = debugfs_create_dir("imc", arch_debugfs_dir);
  53. if (of_property_read_u32(node, "cb_offset", &cb_offset))
  54. cb_offset = IMC_CNTL_BLK_OFFSET;
  55. while (ptr->vbase != NULL) {
  56. loc = (u64)(ptr->vbase) + cb_offset;
  57. imc_mode_addr = (u64 *)(loc + IMC_CNTL_BLK_MODE_OFFSET);
  58. sprintf(mode, "imc_mode_%d", (u32)(ptr->id));
  59. imc_debugfs_create_x64(mode, 0600, imc_debugfs_parent,
  60. imc_mode_addr);
  61. imc_cmd_addr = (u64 *)(loc + IMC_CNTL_BLK_CMD_OFFSET);
  62. sprintf(cmd, "imc_cmd_%d", (u32)(ptr->id));
  63. imc_debugfs_create_x64(cmd, 0600, imc_debugfs_parent,
  64. imc_cmd_addr);
  65. ptr++;
  66. }
  67. }
  68. /*
  69. * imc_get_mem_addr_nest: Function to get nest counter memory region
  70. * for each chip
  71. */
  72. static int imc_get_mem_addr_nest(struct device_node *node,
  73. struct imc_pmu *pmu_ptr,
  74. u32 offset)
  75. {
  76. int nr_chips = 0, i;
  77. u64 *base_addr_arr, baddr;
  78. u32 *chipid_arr;
  79. nr_chips = of_property_count_u32_elems(node, "chip-id");
  80. if (nr_chips <= 0)
  81. return -ENODEV;
  82. base_addr_arr = kcalloc(nr_chips, sizeof(*base_addr_arr), GFP_KERNEL);
  83. if (!base_addr_arr)
  84. return -ENOMEM;
  85. chipid_arr = kcalloc(nr_chips, sizeof(*chipid_arr), GFP_KERNEL);
  86. if (!chipid_arr) {
  87. kfree(base_addr_arr);
  88. return -ENOMEM;
  89. }
  90. if (of_property_read_u32_array(node, "chip-id", chipid_arr, nr_chips))
  91. goto error;
  92. if (of_property_read_u64_array(node, "base-addr", base_addr_arr,
  93. nr_chips))
  94. goto error;
  95. pmu_ptr->mem_info = kcalloc(nr_chips + 1, sizeof(*pmu_ptr->mem_info),
  96. GFP_KERNEL);
  97. if (!pmu_ptr->mem_info)
  98. goto error;
  99. for (i = 0; i < nr_chips; i++) {
  100. pmu_ptr->mem_info[i].id = chipid_arr[i];
  101. baddr = base_addr_arr[i] + offset;
  102. pmu_ptr->mem_info[i].vbase = phys_to_virt(baddr);
  103. }
  104. pmu_ptr->imc_counter_mmaped = true;
  105. kfree(base_addr_arr);
  106. kfree(chipid_arr);
  107. return 0;
  108. error:
  109. kfree(base_addr_arr);
  110. kfree(chipid_arr);
  111. return -1;
  112. }
  113. /*
  114. * imc_pmu_create : Takes the parent device which is the pmu unit, pmu_index
  115. * and domain as the inputs.
  116. * Allocates memory for the struct imc_pmu, sets up its domain, size and offsets
  117. */
  118. static struct imc_pmu *imc_pmu_create(struct device_node *parent, int pmu_index, int domain)
  119. {
  120. int ret = 0;
  121. struct imc_pmu *pmu_ptr;
  122. u32 offset;
  123. /* Return for unknown domain */
  124. if (domain < 0)
  125. return NULL;
  126. /* memory for pmu */
  127. pmu_ptr = kzalloc(sizeof(*pmu_ptr), GFP_KERNEL);
  128. if (!pmu_ptr)
  129. return NULL;
  130. /* Set the domain */
  131. pmu_ptr->domain = domain;
  132. ret = of_property_read_u32(parent, "size", &pmu_ptr->counter_mem_size);
  133. if (ret)
  134. goto free_pmu;
  135. if (!of_property_read_u32(parent, "offset", &offset)) {
  136. if (imc_get_mem_addr_nest(parent, pmu_ptr, offset))
  137. goto free_pmu;
  138. }
  139. /* Function to register IMC pmu */
  140. ret = init_imc_pmu(parent, pmu_ptr, pmu_index);
  141. if (ret) {
  142. pr_err("IMC PMU %s Register failed\n", pmu_ptr->pmu.name);
  143. kfree(pmu_ptr->pmu.name);
  144. if (pmu_ptr->domain == IMC_DOMAIN_NEST)
  145. kfree(pmu_ptr->mem_info);
  146. kfree(pmu_ptr);
  147. return NULL;
  148. }
  149. return pmu_ptr;
  150. free_pmu:
  151. kfree(pmu_ptr);
  152. return NULL;
  153. }
  154. static void disable_nest_pmu_counters(void)
  155. {
  156. int nid, cpu;
  157. const struct cpumask *l_cpumask;
  158. cpus_read_lock();
  159. for_each_node_with_cpus(nid) {
  160. l_cpumask = cpumask_of_node(nid);
  161. cpu = cpumask_first_and(l_cpumask, cpu_online_mask);
  162. if (cpu >= nr_cpu_ids)
  163. continue;
  164. opal_imc_counters_stop(OPAL_IMC_COUNTERS_NEST,
  165. get_hard_smp_processor_id(cpu));
  166. }
  167. cpus_read_unlock();
  168. }
  169. static void disable_core_pmu_counters(void)
  170. {
  171. int cpu, rc;
  172. cpus_read_lock();
  173. /* Disable the IMC Core functions */
  174. for_each_online_cpu(cpu) {
  175. if (cpu_first_thread_sibling(cpu) != cpu)
  176. continue;
  177. rc = opal_imc_counters_stop(OPAL_IMC_COUNTERS_CORE,
  178. get_hard_smp_processor_id(cpu));
  179. if (rc)
  180. pr_err("%s: Failed to stop Core (cpu = %d)\n",
  181. __func__, cpu);
  182. }
  183. cpus_read_unlock();
  184. }
  185. int get_max_nest_dev(void)
  186. {
  187. struct device_node *node;
  188. u32 pmu_units = 0, type;
  189. for_each_compatible_node(node, NULL, IMC_DTB_UNIT_COMPAT) {
  190. if (of_property_read_u32(node, "type", &type))
  191. continue;
  192. if (type == IMC_TYPE_CHIP)
  193. pmu_units++;
  194. }
  195. return pmu_units;
  196. }
  197. static int opal_imc_counters_probe(struct platform_device *pdev)
  198. {
  199. struct device_node *imc_dev = pdev->dev.of_node;
  200. struct imc_pmu *pmu;
  201. int pmu_count = 0, domain;
  202. bool core_imc_reg = false, thread_imc_reg = false;
  203. u32 type;
  204. /*
  205. * Check whether this is kdump kernel. If yes, force the engines to
  206. * stop and return.
  207. */
  208. if (is_kdump_kernel()) {
  209. disable_nest_pmu_counters();
  210. disable_core_pmu_counters();
  211. return -ENODEV;
  212. }
  213. for_each_compatible_node(imc_dev, NULL, IMC_DTB_UNIT_COMPAT) {
  214. pmu = NULL;
  215. if (of_property_read_u32(imc_dev, "type", &type)) {
  216. pr_warn("IMC Device without type property\n");
  217. continue;
  218. }
  219. switch (type) {
  220. case IMC_TYPE_CHIP:
  221. domain = IMC_DOMAIN_NEST;
  222. break;
  223. case IMC_TYPE_CORE:
  224. domain =IMC_DOMAIN_CORE;
  225. break;
  226. case IMC_TYPE_THREAD:
  227. domain = IMC_DOMAIN_THREAD;
  228. break;
  229. case IMC_TYPE_TRACE:
  230. domain = IMC_DOMAIN_TRACE;
  231. break;
  232. default:
  233. pr_warn("IMC Unknown Device type \n");
  234. domain = -1;
  235. break;
  236. }
  237. pmu = imc_pmu_create(imc_dev, pmu_count, domain);
  238. if (pmu != NULL) {
  239. if (domain == IMC_DOMAIN_NEST) {
  240. if (!imc_debugfs_parent)
  241. export_imc_mode_and_cmd(imc_dev, pmu);
  242. pmu_count++;
  243. }
  244. if (domain == IMC_DOMAIN_CORE)
  245. core_imc_reg = true;
  246. if (domain == IMC_DOMAIN_THREAD)
  247. thread_imc_reg = true;
  248. }
  249. }
  250. /* If core imc is not registered, unregister thread-imc */
  251. if (!core_imc_reg && thread_imc_reg)
  252. unregister_thread_imc();
  253. return 0;
  254. }
  255. static void opal_imc_counters_shutdown(struct platform_device *pdev)
  256. {
  257. /*
  258. * Function only stops the engines which is bare minimum.
  259. * TODO: Need to handle proper memory cleanup and pmu
  260. * unregister.
  261. */
  262. disable_nest_pmu_counters();
  263. disable_core_pmu_counters();
  264. }
  265. static const struct of_device_id opal_imc_match[] = {
  266. { .compatible = IMC_DTB_COMPAT },
  267. {},
  268. };
  269. static struct platform_driver opal_imc_driver = {
  270. .driver = {
  271. .name = "opal-imc-counters",
  272. .of_match_table = opal_imc_match,
  273. },
  274. .probe = opal_imc_counters_probe,
  275. .shutdown = opal_imc_counters_shutdown,
  276. };
  277. builtin_platform_driver(opal_imc_driver);