pcpu.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /******************************************************************************
  2. * pcpu.c
  3. * Management physical cpu in dom0, get pcpu info and provide sys interface
  4. *
  5. * Copyright (c) 2012 Intel Corporation
  6. * Author: Liu, Jinsong <jinsong.liu@intel.com>
  7. * Author: Jiang, Yunhong <yunhong.jiang@intel.com>
  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 version 2
  11. * as published by the Free Software Foundation; or, when distributed
  12. * separately from the Linux kernel or incorporated into other
  13. * software packages, subject to the following license:
  14. *
  15. * Permission is hereby granted, free of charge, to any person obtaining a copy
  16. * of this source file (the "Software"), to deal in the Software without
  17. * restriction, including without limitation the rights to use, copy, modify,
  18. * merge, publish, distribute, sublicense, and/or sell copies of the Software,
  19. * and to permit persons to whom the Software is furnished to do so, subject to
  20. * the following conditions:
  21. *
  22. * The above copyright notice and this permission notice shall be included in
  23. * all copies or substantial portions of the Software.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  26. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  27. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  28. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  29. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  30. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  31. * IN THE SOFTWARE.
  32. */
  33. #include <linux/interrupt.h>
  34. #include <linux/spinlock.h>
  35. #include <linux/cpu.h>
  36. #include <linux/stat.h>
  37. #include <linux/capability.h>
  38. #include <xen/xen.h>
  39. #include <xen/xenbus.h>
  40. #include <xen/events.h>
  41. #include <xen/interface/platform.h>
  42. #include <asm/xen/hypervisor.h>
  43. #include <asm/xen/hypercall.h>
  44. #define XEN_PCPU "xen_cpu: "
  45. /*
  46. * @cpu_id: Xen physical cpu logic number
  47. * @flags: Xen physical cpu status flag
  48. * - XEN_PCPU_FLAGS_ONLINE: cpu is online
  49. * - XEN_PCPU_FLAGS_INVALID: cpu is not present
  50. */
  51. struct pcpu {
  52. struct list_head list;
  53. struct device dev;
  54. uint32_t cpu_id;
  55. uint32_t flags;
  56. };
  57. static struct bus_type xen_pcpu_subsys = {
  58. .name = "xen_cpu",
  59. .dev_name = "xen_cpu",
  60. };
  61. static DEFINE_MUTEX(xen_pcpu_lock);
  62. static LIST_HEAD(xen_pcpus);
  63. static int xen_pcpu_down(uint32_t cpu_id)
  64. {
  65. struct xen_platform_op op = {
  66. .cmd = XENPF_cpu_offline,
  67. .interface_version = XENPF_INTERFACE_VERSION,
  68. .u.cpu_ol.cpuid = cpu_id,
  69. };
  70. return HYPERVISOR_dom0_op(&op);
  71. }
  72. static int xen_pcpu_up(uint32_t cpu_id)
  73. {
  74. struct xen_platform_op op = {
  75. .cmd = XENPF_cpu_online,
  76. .interface_version = XENPF_INTERFACE_VERSION,
  77. .u.cpu_ol.cpuid = cpu_id,
  78. };
  79. return HYPERVISOR_dom0_op(&op);
  80. }
  81. static ssize_t show_online(struct device *dev,
  82. struct device_attribute *attr,
  83. char *buf)
  84. {
  85. struct pcpu *cpu = container_of(dev, struct pcpu, dev);
  86. return sprintf(buf, "%u\n", !!(cpu->flags & XEN_PCPU_FLAGS_ONLINE));
  87. }
  88. static ssize_t __ref store_online(struct device *dev,
  89. struct device_attribute *attr,
  90. const char *buf, size_t count)
  91. {
  92. struct pcpu *pcpu = container_of(dev, struct pcpu, dev);
  93. unsigned long long val;
  94. ssize_t ret;
  95. if (!capable(CAP_SYS_ADMIN))
  96. return -EPERM;
  97. if (kstrtoull(buf, 0, &val) < 0)
  98. return -EINVAL;
  99. switch (val) {
  100. case 0:
  101. ret = xen_pcpu_down(pcpu->cpu_id);
  102. break;
  103. case 1:
  104. ret = xen_pcpu_up(pcpu->cpu_id);
  105. break;
  106. default:
  107. ret = -EINVAL;
  108. }
  109. if (ret >= 0)
  110. ret = count;
  111. return ret;
  112. }
  113. static DEVICE_ATTR(online, S_IRUGO | S_IWUSR, show_online, store_online);
  114. static bool xen_pcpu_online(uint32_t flags)
  115. {
  116. return !!(flags & XEN_PCPU_FLAGS_ONLINE);
  117. }
  118. static void pcpu_online_status(struct xenpf_pcpuinfo *info,
  119. struct pcpu *pcpu)
  120. {
  121. if (xen_pcpu_online(info->flags) &&
  122. !xen_pcpu_online(pcpu->flags)) {
  123. /* the pcpu is onlined */
  124. pcpu->flags |= XEN_PCPU_FLAGS_ONLINE;
  125. kobject_uevent(&pcpu->dev.kobj, KOBJ_ONLINE);
  126. } else if (!xen_pcpu_online(info->flags) &&
  127. xen_pcpu_online(pcpu->flags)) {
  128. /* The pcpu is offlined */
  129. pcpu->flags &= ~XEN_PCPU_FLAGS_ONLINE;
  130. kobject_uevent(&pcpu->dev.kobj, KOBJ_OFFLINE);
  131. }
  132. }
  133. static struct pcpu *get_pcpu(uint32_t cpu_id)
  134. {
  135. struct pcpu *pcpu;
  136. list_for_each_entry(pcpu, &xen_pcpus, list) {
  137. if (pcpu->cpu_id == cpu_id)
  138. return pcpu;
  139. }
  140. return NULL;
  141. }
  142. static void pcpu_release(struct device *dev)
  143. {
  144. struct pcpu *pcpu = container_of(dev, struct pcpu, dev);
  145. list_del(&pcpu->list);
  146. kfree(pcpu);
  147. }
  148. static void unregister_and_remove_pcpu(struct pcpu *pcpu)
  149. {
  150. struct device *dev;
  151. if (!pcpu)
  152. return;
  153. dev = &pcpu->dev;
  154. if (dev->id)
  155. device_remove_file(dev, &dev_attr_online);
  156. /* pcpu remove would be implicitly done */
  157. device_unregister(dev);
  158. }
  159. static int register_pcpu(struct pcpu *pcpu)
  160. {
  161. struct device *dev;
  162. int err = -EINVAL;
  163. if (!pcpu)
  164. return err;
  165. dev = &pcpu->dev;
  166. dev->bus = &xen_pcpu_subsys;
  167. dev->id = pcpu->cpu_id;
  168. dev->release = pcpu_release;
  169. err = device_register(dev);
  170. if (err) {
  171. pcpu_release(dev);
  172. return err;
  173. }
  174. /*
  175. * Xen never offline cpu0 due to several restrictions
  176. * and assumptions. This basically doesn't add a sys control
  177. * to user, one cannot attempt to offline BSP.
  178. */
  179. if (dev->id) {
  180. err = device_create_file(dev, &dev_attr_online);
  181. if (err) {
  182. device_unregister(dev);
  183. return err;
  184. }
  185. }
  186. return 0;
  187. }
  188. static struct pcpu *create_and_register_pcpu(struct xenpf_pcpuinfo *info)
  189. {
  190. struct pcpu *pcpu;
  191. int err;
  192. if (info->flags & XEN_PCPU_FLAGS_INVALID)
  193. return ERR_PTR(-ENODEV);
  194. pcpu = kzalloc(sizeof(struct pcpu), GFP_KERNEL);
  195. if (!pcpu)
  196. return ERR_PTR(-ENOMEM);
  197. INIT_LIST_HEAD(&pcpu->list);
  198. pcpu->cpu_id = info->xen_cpuid;
  199. pcpu->flags = info->flags;
  200. /* Need hold on xen_pcpu_lock before pcpu list manipulations */
  201. list_add_tail(&pcpu->list, &xen_pcpus);
  202. err = register_pcpu(pcpu);
  203. if (err) {
  204. pr_warning(XEN_PCPU "Failed to register pcpu%u\n",
  205. info->xen_cpuid);
  206. return ERR_PTR(-ENOENT);
  207. }
  208. return pcpu;
  209. }
  210. /*
  211. * Caller should hold the xen_pcpu_lock
  212. */
  213. static int sync_pcpu(uint32_t cpu, uint32_t *max_cpu)
  214. {
  215. int ret;
  216. struct pcpu *pcpu = NULL;
  217. struct xenpf_pcpuinfo *info;
  218. struct xen_platform_op op = {
  219. .cmd = XENPF_get_cpuinfo,
  220. .interface_version = XENPF_INTERFACE_VERSION,
  221. .u.pcpu_info.xen_cpuid = cpu,
  222. };
  223. ret = HYPERVISOR_dom0_op(&op);
  224. if (ret)
  225. return ret;
  226. info = &op.u.pcpu_info;
  227. if (max_cpu)
  228. *max_cpu = info->max_present;
  229. pcpu = get_pcpu(cpu);
  230. /*
  231. * Only those at cpu present map has its sys interface.
  232. */
  233. if (info->flags & XEN_PCPU_FLAGS_INVALID) {
  234. unregister_and_remove_pcpu(pcpu);
  235. return 0;
  236. }
  237. if (!pcpu) {
  238. pcpu = create_and_register_pcpu(info);
  239. if (IS_ERR_OR_NULL(pcpu))
  240. return -ENODEV;
  241. } else
  242. pcpu_online_status(info, pcpu);
  243. return 0;
  244. }
  245. /*
  246. * Sync dom0's pcpu information with xen hypervisor's
  247. */
  248. static int xen_sync_pcpus(void)
  249. {
  250. /*
  251. * Boot cpu always have cpu_id 0 in xen
  252. */
  253. uint32_t cpu = 0, max_cpu = 0;
  254. int err = 0;
  255. struct pcpu *pcpu, *tmp;
  256. mutex_lock(&xen_pcpu_lock);
  257. while (!err && (cpu <= max_cpu)) {
  258. err = sync_pcpu(cpu, &max_cpu);
  259. cpu++;
  260. }
  261. if (err)
  262. list_for_each_entry_safe(pcpu, tmp, &xen_pcpus, list)
  263. unregister_and_remove_pcpu(pcpu);
  264. mutex_unlock(&xen_pcpu_lock);
  265. return err;
  266. }
  267. static void xen_pcpu_work_fn(struct work_struct *work)
  268. {
  269. xen_sync_pcpus();
  270. }
  271. static DECLARE_WORK(xen_pcpu_work, xen_pcpu_work_fn);
  272. static irqreturn_t xen_pcpu_interrupt(int irq, void *dev_id)
  273. {
  274. schedule_work(&xen_pcpu_work);
  275. return IRQ_HANDLED;
  276. }
  277. /* Sync with Xen hypervisor after cpu hotadded */
  278. void xen_pcpu_hotplug_sync(void)
  279. {
  280. schedule_work(&xen_pcpu_work);
  281. }
  282. EXPORT_SYMBOL_GPL(xen_pcpu_hotplug_sync);
  283. /*
  284. * For hypervisor presented cpu, return logic cpu id;
  285. * For hypervisor non-presented cpu, return -ENODEV.
  286. */
  287. int xen_pcpu_id(uint32_t acpi_id)
  288. {
  289. int cpu_id = 0, max_id = 0;
  290. struct xen_platform_op op;
  291. op.cmd = XENPF_get_cpuinfo;
  292. while (cpu_id <= max_id) {
  293. op.u.pcpu_info.xen_cpuid = cpu_id;
  294. if (HYPERVISOR_dom0_op(&op)) {
  295. cpu_id++;
  296. continue;
  297. }
  298. if (acpi_id == op.u.pcpu_info.acpi_id)
  299. return cpu_id;
  300. if (op.u.pcpu_info.max_present > max_id)
  301. max_id = op.u.pcpu_info.max_present;
  302. cpu_id++;
  303. }
  304. return -ENODEV;
  305. }
  306. EXPORT_SYMBOL_GPL(xen_pcpu_id);
  307. static int __init xen_pcpu_init(void)
  308. {
  309. int irq, ret;
  310. if (!xen_initial_domain())
  311. return -ENODEV;
  312. irq = bind_virq_to_irqhandler(VIRQ_PCPU_STATE, 0,
  313. xen_pcpu_interrupt, 0,
  314. "xen-pcpu", NULL);
  315. if (irq < 0) {
  316. pr_warning(XEN_PCPU "Failed to bind pcpu virq\n");
  317. return irq;
  318. }
  319. ret = subsys_system_register(&xen_pcpu_subsys, NULL);
  320. if (ret) {
  321. pr_warning(XEN_PCPU "Failed to register pcpu subsys\n");
  322. goto err1;
  323. }
  324. ret = xen_sync_pcpus();
  325. if (ret) {
  326. pr_warning(XEN_PCPU "Failed to sync pcpu info\n");
  327. goto err2;
  328. }
  329. return 0;
  330. err2:
  331. bus_unregister(&xen_pcpu_subsys);
  332. err1:
  333. unbind_from_irqhandler(irq, NULL);
  334. return ret;
  335. }
  336. arch_initcall(xen_pcpu_init);