pcpu.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. #define pr_fmt(fmt) "xen_cpu: " fmt
  34. #include <linux/interrupt.h>
  35. #include <linux/spinlock.h>
  36. #include <linux/cpu.h>
  37. #include <linux/stat.h>
  38. #include <linux/capability.h>
  39. #include <xen/xen.h>
  40. #include <xen/acpi.h>
  41. #include <xen/xenbus.h>
  42. #include <xen/events.h>
  43. #include <xen/interface/platform.h>
  44. #include <asm/xen/hypervisor.h>
  45. #include <asm/xen/hypercall.h>
  46. /*
  47. * @cpu_id: Xen physical cpu logic number
  48. * @flags: Xen physical cpu status flag
  49. * - XEN_PCPU_FLAGS_ONLINE: cpu is online
  50. * - XEN_PCPU_FLAGS_INVALID: cpu is not present
  51. */
  52. struct pcpu {
  53. struct list_head list;
  54. struct device dev;
  55. uint32_t cpu_id;
  56. uint32_t flags;
  57. };
  58. static struct bus_type xen_pcpu_subsys = {
  59. .name = "xen_cpu",
  60. .dev_name = "xen_cpu",
  61. };
  62. static DEFINE_MUTEX(xen_pcpu_lock);
  63. static LIST_HEAD(xen_pcpus);
  64. static int xen_pcpu_down(uint32_t cpu_id)
  65. {
  66. struct xen_platform_op op = {
  67. .cmd = XENPF_cpu_offline,
  68. .interface_version = XENPF_INTERFACE_VERSION,
  69. .u.cpu_ol.cpuid = cpu_id,
  70. };
  71. return HYPERVISOR_dom0_op(&op);
  72. }
  73. static int xen_pcpu_up(uint32_t cpu_id)
  74. {
  75. struct xen_platform_op op = {
  76. .cmd = XENPF_cpu_online,
  77. .interface_version = XENPF_INTERFACE_VERSION,
  78. .u.cpu_ol.cpuid = cpu_id,
  79. };
  80. return HYPERVISOR_dom0_op(&op);
  81. }
  82. static ssize_t show_online(struct device *dev,
  83. struct device_attribute *attr,
  84. char *buf)
  85. {
  86. struct pcpu *cpu = container_of(dev, struct pcpu, dev);
  87. return sprintf(buf, "%u\n", !!(cpu->flags & XEN_PCPU_FLAGS_ONLINE));
  88. }
  89. static ssize_t __ref store_online(struct device *dev,
  90. struct device_attribute *attr,
  91. const char *buf, size_t count)
  92. {
  93. struct pcpu *pcpu = container_of(dev, struct pcpu, dev);
  94. unsigned long long val;
  95. ssize_t ret;
  96. if (!capable(CAP_SYS_ADMIN))
  97. return -EPERM;
  98. if (kstrtoull(buf, 0, &val) < 0)
  99. return -EINVAL;
  100. switch (val) {
  101. case 0:
  102. ret = xen_pcpu_down(pcpu->cpu_id);
  103. break;
  104. case 1:
  105. ret = xen_pcpu_up(pcpu->cpu_id);
  106. break;
  107. default:
  108. ret = -EINVAL;
  109. }
  110. if (ret >= 0)
  111. ret = count;
  112. return ret;
  113. }
  114. static DEVICE_ATTR(online, S_IRUGO | S_IWUSR, show_online, store_online);
  115. static struct attribute *pcpu_dev_attrs[] = {
  116. &dev_attr_online.attr,
  117. NULL
  118. };
  119. static umode_t pcpu_dev_is_visible(struct kobject *kobj,
  120. struct attribute *attr, int idx)
  121. {
  122. struct device *dev = kobj_to_dev(kobj);
  123. /*
  124. * Xen never offline cpu0 due to several restrictions
  125. * and assumptions. This basically doesn't add a sys control
  126. * to user, one cannot attempt to offline BSP.
  127. */
  128. return dev->id ? attr->mode : 0;
  129. }
  130. static const struct attribute_group pcpu_dev_group = {
  131. .attrs = pcpu_dev_attrs,
  132. .is_visible = pcpu_dev_is_visible,
  133. };
  134. static const struct attribute_group *pcpu_dev_groups[] = {
  135. &pcpu_dev_group,
  136. NULL
  137. };
  138. static bool xen_pcpu_online(uint32_t flags)
  139. {
  140. return !!(flags & XEN_PCPU_FLAGS_ONLINE);
  141. }
  142. static void pcpu_online_status(struct xenpf_pcpuinfo *info,
  143. struct pcpu *pcpu)
  144. {
  145. if (xen_pcpu_online(info->flags) &&
  146. !xen_pcpu_online(pcpu->flags)) {
  147. /* the pcpu is onlined */
  148. pcpu->flags |= XEN_PCPU_FLAGS_ONLINE;
  149. kobject_uevent(&pcpu->dev.kobj, KOBJ_ONLINE);
  150. } else if (!xen_pcpu_online(info->flags) &&
  151. xen_pcpu_online(pcpu->flags)) {
  152. /* The pcpu is offlined */
  153. pcpu->flags &= ~XEN_PCPU_FLAGS_ONLINE;
  154. kobject_uevent(&pcpu->dev.kobj, KOBJ_OFFLINE);
  155. }
  156. }
  157. static struct pcpu *get_pcpu(uint32_t cpu_id)
  158. {
  159. struct pcpu *pcpu;
  160. list_for_each_entry(pcpu, &xen_pcpus, list) {
  161. if (pcpu->cpu_id == cpu_id)
  162. return pcpu;
  163. }
  164. return NULL;
  165. }
  166. static void pcpu_release(struct device *dev)
  167. {
  168. struct pcpu *pcpu = container_of(dev, struct pcpu, dev);
  169. list_del(&pcpu->list);
  170. kfree(pcpu);
  171. }
  172. static void unregister_and_remove_pcpu(struct pcpu *pcpu)
  173. {
  174. struct device *dev;
  175. if (!pcpu)
  176. return;
  177. dev = &pcpu->dev;
  178. /* pcpu remove would be implicitly done */
  179. device_unregister(dev);
  180. }
  181. static int register_pcpu(struct pcpu *pcpu)
  182. {
  183. struct device *dev;
  184. int err = -EINVAL;
  185. if (!pcpu)
  186. return err;
  187. dev = &pcpu->dev;
  188. dev->bus = &xen_pcpu_subsys;
  189. dev->id = pcpu->cpu_id;
  190. dev->release = pcpu_release;
  191. dev->groups = pcpu_dev_groups;
  192. err = device_register(dev);
  193. if (err) {
  194. pcpu_release(dev);
  195. return err;
  196. }
  197. return 0;
  198. }
  199. static struct pcpu *create_and_register_pcpu(struct xenpf_pcpuinfo *info)
  200. {
  201. struct pcpu *pcpu;
  202. int err;
  203. if (info->flags & XEN_PCPU_FLAGS_INVALID)
  204. return ERR_PTR(-ENODEV);
  205. pcpu = kzalloc(sizeof(struct pcpu), GFP_KERNEL);
  206. if (!pcpu)
  207. return ERR_PTR(-ENOMEM);
  208. INIT_LIST_HEAD(&pcpu->list);
  209. pcpu->cpu_id = info->xen_cpuid;
  210. pcpu->flags = info->flags;
  211. /* Need hold on xen_pcpu_lock before pcpu list manipulations */
  212. list_add_tail(&pcpu->list, &xen_pcpus);
  213. err = register_pcpu(pcpu);
  214. if (err) {
  215. pr_warn("Failed to register pcpu%u\n", info->xen_cpuid);
  216. return ERR_PTR(-ENOENT);
  217. }
  218. return pcpu;
  219. }
  220. /*
  221. * Caller should hold the xen_pcpu_lock
  222. */
  223. static int sync_pcpu(uint32_t cpu, uint32_t *max_cpu)
  224. {
  225. int ret;
  226. struct pcpu *pcpu = NULL;
  227. struct xenpf_pcpuinfo *info;
  228. struct xen_platform_op op = {
  229. .cmd = XENPF_get_cpuinfo,
  230. .interface_version = XENPF_INTERFACE_VERSION,
  231. .u.pcpu_info.xen_cpuid = cpu,
  232. };
  233. ret = HYPERVISOR_dom0_op(&op);
  234. if (ret)
  235. return ret;
  236. info = &op.u.pcpu_info;
  237. if (max_cpu)
  238. *max_cpu = info->max_present;
  239. pcpu = get_pcpu(cpu);
  240. /*
  241. * Only those at cpu present map has its sys interface.
  242. */
  243. if (info->flags & XEN_PCPU_FLAGS_INVALID) {
  244. unregister_and_remove_pcpu(pcpu);
  245. return 0;
  246. }
  247. if (!pcpu) {
  248. pcpu = create_and_register_pcpu(info);
  249. if (IS_ERR_OR_NULL(pcpu))
  250. return -ENODEV;
  251. } else
  252. pcpu_online_status(info, pcpu);
  253. return 0;
  254. }
  255. /*
  256. * Sync dom0's pcpu information with xen hypervisor's
  257. */
  258. static int xen_sync_pcpus(void)
  259. {
  260. /*
  261. * Boot cpu always have cpu_id 0 in xen
  262. */
  263. uint32_t cpu = 0, max_cpu = 0;
  264. int err = 0;
  265. struct pcpu *pcpu, *tmp;
  266. mutex_lock(&xen_pcpu_lock);
  267. while (!err && (cpu <= max_cpu)) {
  268. err = sync_pcpu(cpu, &max_cpu);
  269. cpu++;
  270. }
  271. if (err)
  272. list_for_each_entry_safe(pcpu, tmp, &xen_pcpus, list)
  273. unregister_and_remove_pcpu(pcpu);
  274. mutex_unlock(&xen_pcpu_lock);
  275. return err;
  276. }
  277. static void xen_pcpu_work_fn(struct work_struct *work)
  278. {
  279. xen_sync_pcpus();
  280. }
  281. static DECLARE_WORK(xen_pcpu_work, xen_pcpu_work_fn);
  282. static irqreturn_t xen_pcpu_interrupt(int irq, void *dev_id)
  283. {
  284. schedule_work(&xen_pcpu_work);
  285. return IRQ_HANDLED;
  286. }
  287. /* Sync with Xen hypervisor after cpu hotadded */
  288. void xen_pcpu_hotplug_sync(void)
  289. {
  290. schedule_work(&xen_pcpu_work);
  291. }
  292. EXPORT_SYMBOL_GPL(xen_pcpu_hotplug_sync);
  293. /*
  294. * For hypervisor presented cpu, return logic cpu id;
  295. * For hypervisor non-presented cpu, return -ENODEV.
  296. */
  297. int xen_pcpu_id(uint32_t acpi_id)
  298. {
  299. int cpu_id = 0, max_id = 0;
  300. struct xen_platform_op op;
  301. op.cmd = XENPF_get_cpuinfo;
  302. while (cpu_id <= max_id) {
  303. op.u.pcpu_info.xen_cpuid = cpu_id;
  304. if (HYPERVISOR_dom0_op(&op)) {
  305. cpu_id++;
  306. continue;
  307. }
  308. if (acpi_id == op.u.pcpu_info.acpi_id)
  309. return cpu_id;
  310. if (op.u.pcpu_info.max_present > max_id)
  311. max_id = op.u.pcpu_info.max_present;
  312. cpu_id++;
  313. }
  314. return -ENODEV;
  315. }
  316. EXPORT_SYMBOL_GPL(xen_pcpu_id);
  317. static int __init xen_pcpu_init(void)
  318. {
  319. int irq, ret;
  320. if (!xen_initial_domain())
  321. return -ENODEV;
  322. irq = bind_virq_to_irqhandler(VIRQ_PCPU_STATE, 0,
  323. xen_pcpu_interrupt, 0,
  324. "xen-pcpu", NULL);
  325. if (irq < 0) {
  326. pr_warn("Failed to bind pcpu virq\n");
  327. return irq;
  328. }
  329. ret = subsys_system_register(&xen_pcpu_subsys, NULL);
  330. if (ret) {
  331. pr_warn("Failed to register pcpu subsys\n");
  332. goto err1;
  333. }
  334. ret = xen_sync_pcpus();
  335. if (ret) {
  336. pr_warn("Failed to sync pcpu info\n");
  337. goto err2;
  338. }
  339. return 0;
  340. err2:
  341. bus_unregister(&xen_pcpu_subsys);
  342. err1:
  343. unbind_from_irqhandler(irq, NULL);
  344. return ret;
  345. }
  346. arch_initcall(xen_pcpu_init);