opal-imc.c 7.9 KB

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