pcpu.c 9.3 KB

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