opal-imc.c 7.6 KB

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