opal-imc.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. int chip = 0, nid;
  56. char mode[16], cmd[16];
  57. u32 cb_offset;
  58. imc_debugfs_parent = debugfs_create_dir("imc", powerpc_debugfs_root);
  59. /*
  60. * Return here, either because 'imc' directory already exists,
  61. * Or failed to create a new one.
  62. */
  63. if (!imc_debugfs_parent)
  64. return;
  65. if (of_property_read_u32(node, "cb_offset", &cb_offset))
  66. cb_offset = IMC_CNTL_BLK_OFFSET;
  67. for_each_node(nid) {
  68. loc = (u64)(pmu_ptr->mem_info[chip].vbase) + cb_offset;
  69. imc_mode_addr = (u64 *)(loc + IMC_CNTL_BLK_MODE_OFFSET);
  70. sprintf(mode, "imc_mode_%d", nid);
  71. if (!imc_debugfs_create_x64(mode, 0600, imc_debugfs_parent,
  72. imc_mode_addr))
  73. goto err;
  74. imc_cmd_addr = (u64 *)(loc + IMC_CNTL_BLK_CMD_OFFSET);
  75. sprintf(cmd, "imc_cmd_%d", nid);
  76. if (!imc_debugfs_create_x64(cmd, 0600, imc_debugfs_parent,
  77. imc_cmd_addr))
  78. goto err;
  79. chip++;
  80. }
  81. return;
  82. err:
  83. debugfs_remove_recursive(imc_debugfs_parent);
  84. }
  85. /*
  86. * imc_get_mem_addr_nest: Function to get nest counter memory region
  87. * for each chip
  88. */
  89. static int imc_get_mem_addr_nest(struct device_node *node,
  90. struct imc_pmu *pmu_ptr,
  91. u32 offset)
  92. {
  93. int nr_chips = 0, i;
  94. u64 *base_addr_arr, baddr;
  95. u32 *chipid_arr;
  96. nr_chips = of_property_count_u32_elems(node, "chip-id");
  97. if (nr_chips <= 0)
  98. return -ENODEV;
  99. base_addr_arr = kcalloc(nr_chips, sizeof(*base_addr_arr), GFP_KERNEL);
  100. if (!base_addr_arr)
  101. return -ENOMEM;
  102. chipid_arr = kcalloc(nr_chips, sizeof(*chipid_arr), GFP_KERNEL);
  103. if (!chipid_arr) {
  104. kfree(base_addr_arr);
  105. return -ENOMEM;
  106. }
  107. if (of_property_read_u32_array(node, "chip-id", chipid_arr, nr_chips))
  108. goto error;
  109. if (of_property_read_u64_array(node, "base-addr", base_addr_arr,
  110. nr_chips))
  111. goto error;
  112. pmu_ptr->mem_info = kcalloc(nr_chips, sizeof(*pmu_ptr->mem_info),
  113. GFP_KERNEL);
  114. if (!pmu_ptr->mem_info)
  115. goto error;
  116. for (i = 0; i < nr_chips; i++) {
  117. pmu_ptr->mem_info[i].id = chipid_arr[i];
  118. baddr = base_addr_arr[i] + offset;
  119. pmu_ptr->mem_info[i].vbase = phys_to_virt(baddr);
  120. }
  121. pmu_ptr->imc_counter_mmaped = true;
  122. export_imc_mode_and_cmd(node, pmu_ptr);
  123. kfree(base_addr_arr);
  124. kfree(chipid_arr);
  125. return 0;
  126. error:
  127. kfree(base_addr_arr);
  128. kfree(chipid_arr);
  129. return -1;
  130. }
  131. /*
  132. * imc_pmu_create : Takes the parent device which is the pmu unit, pmu_index
  133. * and domain as the inputs.
  134. * Allocates memory for the struct imc_pmu, sets up its domain, size and offsets
  135. */
  136. static int imc_pmu_create(struct device_node *parent, int pmu_index, int domain)
  137. {
  138. int ret = 0;
  139. struct imc_pmu *pmu_ptr;
  140. u32 offset;
  141. /* memory for pmu */
  142. pmu_ptr = kzalloc(sizeof(*pmu_ptr), GFP_KERNEL);
  143. if (!pmu_ptr)
  144. return -ENOMEM;
  145. /* Set the domain */
  146. pmu_ptr->domain = domain;
  147. ret = of_property_read_u32(parent, "size", &pmu_ptr->counter_mem_size);
  148. if (ret) {
  149. ret = -EINVAL;
  150. goto free_pmu;
  151. }
  152. if (!of_property_read_u32(parent, "offset", &offset)) {
  153. if (imc_get_mem_addr_nest(parent, pmu_ptr, offset)) {
  154. ret = -EINVAL;
  155. goto free_pmu;
  156. }
  157. }
  158. /* Function to register IMC pmu */
  159. ret = init_imc_pmu(parent, pmu_ptr, pmu_index);
  160. if (ret) {
  161. pr_err("IMC PMU %s Register failed\n", pmu_ptr->pmu.name);
  162. kfree(pmu_ptr->pmu.name);
  163. if (pmu_ptr->domain == IMC_DOMAIN_NEST)
  164. kfree(pmu_ptr->mem_info);
  165. kfree(pmu_ptr);
  166. return ret;
  167. }
  168. return 0;
  169. free_pmu:
  170. kfree(pmu_ptr);
  171. return ret;
  172. }
  173. static void disable_nest_pmu_counters(void)
  174. {
  175. int nid, cpu;
  176. const struct cpumask *l_cpumask;
  177. get_online_cpus();
  178. for_each_node_with_cpus(nid) {
  179. l_cpumask = cpumask_of_node(nid);
  180. cpu = cpumask_first_and(l_cpumask, cpu_online_mask);
  181. if (cpu >= nr_cpu_ids)
  182. continue;
  183. opal_imc_counters_stop(OPAL_IMC_COUNTERS_NEST,
  184. get_hard_smp_processor_id(cpu));
  185. }
  186. put_online_cpus();
  187. }
  188. static void disable_core_pmu_counters(void)
  189. {
  190. cpumask_t cores_map;
  191. int cpu, rc;
  192. get_online_cpus();
  193. /* Disable the IMC Core functions */
  194. cores_map = cpu_online_cores_map();
  195. for_each_cpu(cpu, &cores_map) {
  196. rc = opal_imc_counters_stop(OPAL_IMC_COUNTERS_CORE,
  197. get_hard_smp_processor_id(cpu));
  198. if (rc)
  199. pr_err("%s: Failed to stop Core (cpu = %d)\n",
  200. __FUNCTION__, cpu);
  201. }
  202. put_online_cpus();
  203. }
  204. int get_max_nest_dev(void)
  205. {
  206. struct device_node *node;
  207. u32 pmu_units = 0, type;
  208. for_each_compatible_node(node, NULL, IMC_DTB_UNIT_COMPAT) {
  209. if (of_property_read_u32(node, "type", &type))
  210. continue;
  211. if (type == IMC_TYPE_CHIP)
  212. pmu_units++;
  213. }
  214. return pmu_units;
  215. }
  216. static int opal_imc_counters_probe(struct platform_device *pdev)
  217. {
  218. struct device_node *imc_dev = pdev->dev.of_node;
  219. int pmu_count = 0, domain;
  220. bool core_imc_reg = false, thread_imc_reg = false;
  221. u32 type;
  222. /*
  223. * Check whether this is kdump kernel. If yes, force the engines to
  224. * stop and return.
  225. */
  226. if (is_kdump_kernel()) {
  227. disable_nest_pmu_counters();
  228. disable_core_pmu_counters();
  229. return -ENODEV;
  230. }
  231. for_each_compatible_node(imc_dev, NULL, IMC_DTB_UNIT_COMPAT) {
  232. if (of_property_read_u32(imc_dev, "type", &type)) {
  233. pr_warn("IMC Device without type property\n");
  234. continue;
  235. }
  236. switch (type) {
  237. case IMC_TYPE_CHIP:
  238. domain = IMC_DOMAIN_NEST;
  239. break;
  240. case IMC_TYPE_CORE:
  241. domain =IMC_DOMAIN_CORE;
  242. break;
  243. case IMC_TYPE_THREAD:
  244. domain = IMC_DOMAIN_THREAD;
  245. break;
  246. default:
  247. pr_warn("IMC Unknown Device type \n");
  248. domain = -1;
  249. break;
  250. }
  251. if (!imc_pmu_create(imc_dev, pmu_count, domain)) {
  252. if (domain == IMC_DOMAIN_NEST)
  253. pmu_count++;
  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 none of the nest units are registered, remove debugfs interface */
  261. if (pmu_count == 0)
  262. debugfs_remove_recursive(imc_debugfs_parent);
  263. /* If core imc is not registered, unregister thread-imc */
  264. if (!core_imc_reg && thread_imc_reg)
  265. unregister_thread_imc();
  266. return 0;
  267. }
  268. static void opal_imc_counters_shutdown(struct platform_device *pdev)
  269. {
  270. /*
  271. * Function only stops the engines which is bare minimum.
  272. * TODO: Need to handle proper memory cleanup and pmu
  273. * unregister.
  274. */
  275. disable_nest_pmu_counters();
  276. disable_core_pmu_counters();
  277. }
  278. static const struct of_device_id opal_imc_match[] = {
  279. { .compatible = IMC_DTB_COMPAT },
  280. {},
  281. };
  282. static struct platform_driver opal_imc_driver = {
  283. .driver = {
  284. .name = "opal-imc-counters",
  285. .of_match_table = opal_imc_match,
  286. },
  287. .probe = opal_imc_counters_probe,
  288. .shutdown = opal_imc_counters_shutdown,
  289. };
  290. builtin_platform_driver(opal_imc_driver);