processor_driver.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  1. /*
  2. * acpi_processor.c - ACPI Processor Driver ($Revision: 71 $)
  3. *
  4. * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
  5. * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
  6. * Copyright (C) 2004 Dominik Brodowski <linux@brodo.de>
  7. * Copyright (C) 2004 Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
  8. * - Added processor hotplug support
  9. *
  10. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or (at
  15. * your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful, but
  18. * WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License along
  23. * with this program; if not, write to the Free Software Foundation, Inc.,
  24. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  25. *
  26. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  27. * TBD:
  28. * 1. Make # power states dynamic.
  29. * 2. Support duty_cycle values that span bit 4.
  30. * 3. Optimize by having scheduler determine business instead of
  31. * having us try to calculate it here.
  32. * 4. Need C1 timing -- must modify kernel (IRQ handler) to get this.
  33. */
  34. #include <linux/kernel.h>
  35. #include <linux/module.h>
  36. #include <linux/init.h>
  37. #include <linux/types.h>
  38. #include <linux/pci.h>
  39. #include <linux/pm.h>
  40. #include <linux/cpufreq.h>
  41. #include <linux/cpu.h>
  42. #include <linux/dmi.h>
  43. #include <linux/moduleparam.h>
  44. #include <linux/cpuidle.h>
  45. #include <linux/slab.h>
  46. #include <linux/acpi.h>
  47. #include <linux/memory_hotplug.h>
  48. #include <asm/io.h>
  49. #include <asm/cpu.h>
  50. #include <asm/delay.h>
  51. #include <asm/uaccess.h>
  52. #include <asm/processor.h>
  53. #include <asm/smp.h>
  54. #include <asm/acpi.h>
  55. #include <acpi/acpi_bus.h>
  56. #include <acpi/acpi_drivers.h>
  57. #include <acpi/processor.h>
  58. #define PREFIX "ACPI: "
  59. #define ACPI_PROCESSOR_CLASS "processor"
  60. #define ACPI_PROCESSOR_DEVICE_NAME "Processor"
  61. #define ACPI_PROCESSOR_FILE_INFO "info"
  62. #define ACPI_PROCESSOR_FILE_THROTTLING "throttling"
  63. #define ACPI_PROCESSOR_FILE_LIMIT "limit"
  64. #define ACPI_PROCESSOR_NOTIFY_PERFORMANCE 0x80
  65. #define ACPI_PROCESSOR_NOTIFY_POWER 0x81
  66. #define ACPI_PROCESSOR_NOTIFY_THROTTLING 0x82
  67. #define ACPI_PROCESSOR_DEVICE_HID "ACPI0007"
  68. #define ACPI_PROCESSOR_LIMIT_USER 0
  69. #define ACPI_PROCESSOR_LIMIT_THERMAL 1
  70. #define _COMPONENT ACPI_PROCESSOR_COMPONENT
  71. ACPI_MODULE_NAME("processor_driver");
  72. MODULE_AUTHOR("Paul Diefenbaugh");
  73. MODULE_DESCRIPTION("ACPI Processor Driver");
  74. MODULE_LICENSE("GPL");
  75. static int acpi_processor_add(struct acpi_device *device);
  76. static int acpi_processor_remove(struct acpi_device *device);
  77. static void acpi_processor_notify(struct acpi_device *device, u32 event);
  78. static acpi_status acpi_processor_hotadd_init(struct acpi_processor *pr);
  79. static int acpi_processor_handle_eject(struct acpi_processor *pr);
  80. static int acpi_processor_start(struct acpi_processor *pr);
  81. static const struct acpi_device_id processor_device_ids[] = {
  82. {ACPI_PROCESSOR_OBJECT_HID, 0},
  83. {ACPI_PROCESSOR_DEVICE_HID, 0},
  84. {"", 0},
  85. };
  86. MODULE_DEVICE_TABLE(acpi, processor_device_ids);
  87. static SIMPLE_DEV_PM_OPS(acpi_processor_pm,
  88. acpi_processor_suspend, acpi_processor_resume);
  89. static struct acpi_driver acpi_processor_driver = {
  90. .name = "processor",
  91. .class = ACPI_PROCESSOR_CLASS,
  92. .ids = processor_device_ids,
  93. .ops = {
  94. .add = acpi_processor_add,
  95. .remove = acpi_processor_remove,
  96. .notify = acpi_processor_notify,
  97. },
  98. .drv.pm = &acpi_processor_pm,
  99. };
  100. #define INSTALL_NOTIFY_HANDLER 1
  101. #define UNINSTALL_NOTIFY_HANDLER 2
  102. DEFINE_PER_CPU(struct acpi_processor *, processors);
  103. EXPORT_PER_CPU_SYMBOL(processors);
  104. struct acpi_processor_errata errata __read_mostly;
  105. /* --------------------------------------------------------------------------
  106. Errata Handling
  107. -------------------------------------------------------------------------- */
  108. static int acpi_processor_errata_piix4(struct pci_dev *dev)
  109. {
  110. u8 value1 = 0;
  111. u8 value2 = 0;
  112. if (!dev)
  113. return -EINVAL;
  114. /*
  115. * Note that 'dev' references the PIIX4 ACPI Controller.
  116. */
  117. switch (dev->revision) {
  118. case 0:
  119. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found PIIX4 A-step\n"));
  120. break;
  121. case 1:
  122. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found PIIX4 B-step\n"));
  123. break;
  124. case 2:
  125. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found PIIX4E\n"));
  126. break;
  127. case 3:
  128. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found PIIX4M\n"));
  129. break;
  130. default:
  131. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found unknown PIIX4\n"));
  132. break;
  133. }
  134. switch (dev->revision) {
  135. case 0: /* PIIX4 A-step */
  136. case 1: /* PIIX4 B-step */
  137. /*
  138. * See specification changes #13 ("Manual Throttle Duty Cycle")
  139. * and #14 ("Enabling and Disabling Manual Throttle"), plus
  140. * erratum #5 ("STPCLK# Deassertion Time") from the January
  141. * 2002 PIIX4 specification update. Applies to only older
  142. * PIIX4 models.
  143. */
  144. errata.piix4.throttle = 1;
  145. case 2: /* PIIX4E */
  146. case 3: /* PIIX4M */
  147. /*
  148. * See erratum #18 ("C3 Power State/BMIDE and Type-F DMA
  149. * Livelock") from the January 2002 PIIX4 specification update.
  150. * Applies to all PIIX4 models.
  151. */
  152. /*
  153. * BM-IDE
  154. * ------
  155. * Find the PIIX4 IDE Controller and get the Bus Master IDE
  156. * Status register address. We'll use this later to read
  157. * each IDE controller's DMA status to make sure we catch all
  158. * DMA activity.
  159. */
  160. dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
  161. PCI_DEVICE_ID_INTEL_82371AB,
  162. PCI_ANY_ID, PCI_ANY_ID, NULL);
  163. if (dev) {
  164. errata.piix4.bmisx = pci_resource_start(dev, 4);
  165. pci_dev_put(dev);
  166. }
  167. /*
  168. * Type-F DMA
  169. * ----------
  170. * Find the PIIX4 ISA Controller and read the Motherboard
  171. * DMA controller's status to see if Type-F (Fast) DMA mode
  172. * is enabled (bit 7) on either channel. Note that we'll
  173. * disable C3 support if this is enabled, as some legacy
  174. * devices won't operate well if fast DMA is disabled.
  175. */
  176. dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
  177. PCI_DEVICE_ID_INTEL_82371AB_0,
  178. PCI_ANY_ID, PCI_ANY_ID, NULL);
  179. if (dev) {
  180. pci_read_config_byte(dev, 0x76, &value1);
  181. pci_read_config_byte(dev, 0x77, &value2);
  182. if ((value1 & 0x80) || (value2 & 0x80))
  183. errata.piix4.fdma = 1;
  184. pci_dev_put(dev);
  185. }
  186. break;
  187. }
  188. if (errata.piix4.bmisx)
  189. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  190. "Bus master activity detection (BM-IDE) erratum enabled\n"));
  191. if (errata.piix4.fdma)
  192. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  193. "Type-F DMA livelock erratum (C3 disabled)\n"));
  194. return 0;
  195. }
  196. static int acpi_processor_errata(struct acpi_processor *pr)
  197. {
  198. int result = 0;
  199. struct pci_dev *dev = NULL;
  200. if (!pr)
  201. return -EINVAL;
  202. /*
  203. * PIIX4
  204. */
  205. dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
  206. PCI_DEVICE_ID_INTEL_82371AB_3, PCI_ANY_ID,
  207. PCI_ANY_ID, NULL);
  208. if (dev) {
  209. result = acpi_processor_errata_piix4(dev);
  210. pci_dev_put(dev);
  211. }
  212. return result;
  213. }
  214. /* --------------------------------------------------------------------------
  215. Driver Interface
  216. -------------------------------------------------------------------------- */
  217. static int acpi_processor_get_info(struct acpi_device *device)
  218. {
  219. acpi_status status = 0;
  220. union acpi_object object = { 0 };
  221. struct acpi_buffer buffer = { sizeof(union acpi_object), &object };
  222. struct acpi_processor *pr;
  223. int cpu_index, device_declaration = 0;
  224. static int cpu0_initialized;
  225. pr = acpi_driver_data(device);
  226. if (!pr)
  227. return -EINVAL;
  228. if (num_online_cpus() > 1)
  229. errata.smp = TRUE;
  230. acpi_processor_errata(pr);
  231. /*
  232. * Check to see if we have bus mastering arbitration control. This
  233. * is required for proper C3 usage (to maintain cache coherency).
  234. */
  235. if (acpi_gbl_FADT.pm2_control_block && acpi_gbl_FADT.pm2_control_length) {
  236. pr->flags.bm_control = 1;
  237. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  238. "Bus mastering arbitration control present\n"));
  239. } else
  240. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  241. "No bus mastering arbitration control\n"));
  242. if (!strcmp(acpi_device_hid(device), ACPI_PROCESSOR_OBJECT_HID)) {
  243. /* Declared with "Processor" statement; match ProcessorID */
  244. status = acpi_evaluate_object(pr->handle, NULL, NULL, &buffer);
  245. if (ACPI_FAILURE(status)) {
  246. dev_err(&device->dev,
  247. "Failed to evaluate processor object (0x%x)\n",
  248. status);
  249. return -ENODEV;
  250. }
  251. /*
  252. * TBD: Synch processor ID (via LAPIC/LSAPIC structures) on SMP.
  253. * >>> 'acpi_get_processor_id(acpi_id, &id)' in
  254. * arch/xxx/acpi.c
  255. */
  256. pr->acpi_id = object.processor.proc_id;
  257. } else {
  258. /*
  259. * Declared with "Device" statement; match _UID.
  260. * Note that we don't handle string _UIDs yet.
  261. */
  262. unsigned long long value;
  263. status = acpi_evaluate_integer(pr->handle, METHOD_NAME__UID,
  264. NULL, &value);
  265. if (ACPI_FAILURE(status)) {
  266. dev_err(&device->dev,
  267. "Failed to evaluate processor _UID (0x%x)\n",
  268. status);
  269. return -ENODEV;
  270. }
  271. device_declaration = 1;
  272. pr->acpi_id = value;
  273. }
  274. cpu_index = acpi_get_cpuid(pr->handle, device_declaration, pr->acpi_id);
  275. /* Handle UP system running SMP kernel, with no LAPIC in MADT */
  276. if (!cpu0_initialized && (cpu_index == -1) &&
  277. (num_online_cpus() == 1)) {
  278. cpu_index = 0;
  279. }
  280. cpu0_initialized = 1;
  281. pr->id = cpu_index;
  282. /*
  283. * Extra Processor objects may be enumerated on MP systems with
  284. * less than the max # of CPUs. They should be ignored _iff
  285. * they are physically not present.
  286. */
  287. if (pr->id == -1) {
  288. if (ACPI_FAILURE(acpi_processor_hotadd_init(pr)))
  289. return -ENODEV;
  290. }
  291. /*
  292. * On some boxes several processors use the same processor bus id.
  293. * But they are located in different scope. For example:
  294. * \_SB.SCK0.CPU0
  295. * \_SB.SCK1.CPU0
  296. * Rename the processor device bus id. And the new bus id will be
  297. * generated as the following format:
  298. * CPU+CPU ID.
  299. */
  300. sprintf(acpi_device_bid(device), "CPU%X", pr->id);
  301. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Processor [%d:%d]\n", pr->id,
  302. pr->acpi_id));
  303. if (!object.processor.pblk_address)
  304. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No PBLK (NULL address)\n"));
  305. else if (object.processor.pblk_length != 6)
  306. dev_err(&device->dev, "Invalid PBLK length [%d]\n",
  307. object.processor.pblk_length);
  308. else {
  309. pr->throttling.address = object.processor.pblk_address;
  310. pr->throttling.duty_offset = acpi_gbl_FADT.duty_offset;
  311. pr->throttling.duty_width = acpi_gbl_FADT.duty_width;
  312. pr->pblk = object.processor.pblk_address;
  313. /*
  314. * We don't care about error returns - we just try to mark
  315. * these reserved so that nobody else is confused into thinking
  316. * that this region might be unused..
  317. *
  318. * (In particular, allocating the IO range for Cardbus)
  319. */
  320. request_region(pr->throttling.address, 6, "ACPI CPU throttle");
  321. }
  322. /*
  323. * If ACPI describes a slot number for this CPU, we can use it
  324. * ensure we get the right value in the "physical id" field
  325. * of /proc/cpuinfo
  326. */
  327. status = acpi_evaluate_object(pr->handle, "_SUN", NULL, &buffer);
  328. if (ACPI_SUCCESS(status))
  329. arch_fix_phys_package_id(pr->id, object.integer.value);
  330. return 0;
  331. }
  332. static DEFINE_PER_CPU(void *, processor_device_array);
  333. static void acpi_processor_notify(struct acpi_device *device, u32 event)
  334. {
  335. struct acpi_processor *pr = acpi_driver_data(device);
  336. int saved;
  337. if (!pr)
  338. return;
  339. switch (event) {
  340. case ACPI_PROCESSOR_NOTIFY_PERFORMANCE:
  341. saved = pr->performance_platform_limit;
  342. acpi_processor_ppc_has_changed(pr, 1);
  343. if (saved == pr->performance_platform_limit)
  344. break;
  345. acpi_bus_generate_proc_event(device, event,
  346. pr->performance_platform_limit);
  347. acpi_bus_generate_netlink_event(device->pnp.device_class,
  348. dev_name(&device->dev), event,
  349. pr->performance_platform_limit);
  350. break;
  351. case ACPI_PROCESSOR_NOTIFY_POWER:
  352. acpi_processor_cst_has_changed(pr);
  353. acpi_bus_generate_proc_event(device, event, 0);
  354. acpi_bus_generate_netlink_event(device->pnp.device_class,
  355. dev_name(&device->dev), event, 0);
  356. break;
  357. case ACPI_PROCESSOR_NOTIFY_THROTTLING:
  358. acpi_processor_tstate_has_changed(pr);
  359. acpi_bus_generate_proc_event(device, event, 0);
  360. acpi_bus_generate_netlink_event(device->pnp.device_class,
  361. dev_name(&device->dev), event, 0);
  362. break;
  363. default:
  364. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  365. "Unsupported event [0x%x]\n", event));
  366. break;
  367. }
  368. return;
  369. }
  370. static int acpi_cpu_soft_notify(struct notifier_block *nfb,
  371. unsigned long action, void *hcpu)
  372. {
  373. unsigned int cpu = (unsigned long)hcpu;
  374. struct acpi_processor *pr = per_cpu(processors, cpu);
  375. if (action == CPU_ONLINE && pr) {
  376. /* CPU got physically hotplugged and onlined the first time:
  377. * Initialize missing things
  378. */
  379. if (pr->flags.need_hotplug_init) {
  380. pr_info("Will online and init hotplugged CPU: %d\n",
  381. pr->id);
  382. WARN(acpi_processor_start(pr), "Failed to start CPU:"
  383. " %d\n", pr->id);
  384. pr->flags.need_hotplug_init = 0;
  385. /* Normal CPU soft online event */
  386. } else {
  387. acpi_processor_ppc_has_changed(pr, 0);
  388. acpi_processor_hotplug(pr);
  389. acpi_processor_reevaluate_tstate(pr, action);
  390. acpi_processor_tstate_has_changed(pr);
  391. }
  392. }
  393. if (action == CPU_DEAD && pr) {
  394. /* invalidate the flag.throttling after one CPU is offline */
  395. acpi_processor_reevaluate_tstate(pr, action);
  396. }
  397. return NOTIFY_OK;
  398. }
  399. static struct notifier_block acpi_cpu_notifier =
  400. {
  401. .notifier_call = acpi_cpu_soft_notify,
  402. };
  403. /*
  404. * acpi_processor_start() is called by the cpu_hotplug_notifier func:
  405. * acpi_cpu_soft_notify(). Getting it __cpuinit{data} is difficult, the
  406. * root cause seem to be that acpi_processor_uninstall_hotplug_notify()
  407. * is in the module_exit (__exit) func. Allowing acpi_processor_start()
  408. * to not be in __cpuinit section, but being called from __cpuinit funcs
  409. * via __ref looks like the right thing to do here.
  410. */
  411. static __ref int acpi_processor_start(struct acpi_processor *pr)
  412. {
  413. struct acpi_device *device = per_cpu(processor_device_array, pr->id);
  414. int result = 0;
  415. #ifdef CONFIG_CPU_FREQ
  416. acpi_processor_ppc_has_changed(pr, 0);
  417. acpi_processor_load_module(pr);
  418. #endif
  419. acpi_processor_get_throttling_info(pr);
  420. acpi_processor_get_limit_info(pr);
  421. if (!cpuidle_get_driver() || cpuidle_get_driver() == &acpi_idle_driver)
  422. acpi_processor_power_init(pr);
  423. pr->cdev = thermal_cooling_device_register("Processor", device,
  424. &processor_cooling_ops);
  425. if (IS_ERR(pr->cdev)) {
  426. result = PTR_ERR(pr->cdev);
  427. goto err_power_exit;
  428. }
  429. dev_dbg(&device->dev, "registered as cooling_device%d\n",
  430. pr->cdev->id);
  431. result = sysfs_create_link(&device->dev.kobj,
  432. &pr->cdev->device.kobj,
  433. "thermal_cooling");
  434. if (result) {
  435. dev_err(&device->dev,
  436. "Failed to create sysfs link 'thermal_cooling'\n");
  437. goto err_thermal_unregister;
  438. }
  439. result = sysfs_create_link(&pr->cdev->device.kobj,
  440. &device->dev.kobj,
  441. "device");
  442. if (result) {
  443. dev_err(&pr->cdev->device,
  444. "Failed to create sysfs link 'device'\n");
  445. goto err_remove_sysfs_thermal;
  446. }
  447. return 0;
  448. err_remove_sysfs_thermal:
  449. sysfs_remove_link(&device->dev.kobj, "thermal_cooling");
  450. err_thermal_unregister:
  451. thermal_cooling_device_unregister(pr->cdev);
  452. err_power_exit:
  453. acpi_processor_power_exit(pr);
  454. return result;
  455. }
  456. /*
  457. * Do not put anything in here which needs the core to be online.
  458. * For example MSR access or setting up things which check for cpuinfo_x86
  459. * (cpu_data(cpu)) values, like CPU feature flags, family, model, etc.
  460. * Such things have to be put in and set up above in acpi_processor_start()
  461. */
  462. static int __cpuinit acpi_processor_add(struct acpi_device *device)
  463. {
  464. struct acpi_processor *pr = NULL;
  465. int result = 0;
  466. struct device *dev;
  467. pr = kzalloc(sizeof(struct acpi_processor), GFP_KERNEL);
  468. if (!pr)
  469. return -ENOMEM;
  470. if (!zalloc_cpumask_var(&pr->throttling.shared_cpu_map, GFP_KERNEL)) {
  471. result = -ENOMEM;
  472. goto err_free_pr;
  473. }
  474. pr->handle = device->handle;
  475. strcpy(acpi_device_name(device), ACPI_PROCESSOR_DEVICE_NAME);
  476. strcpy(acpi_device_class(device), ACPI_PROCESSOR_CLASS);
  477. device->driver_data = pr;
  478. result = acpi_processor_get_info(device);
  479. if (result) {
  480. /* Processor is physically not present */
  481. return 0;
  482. }
  483. #ifdef CONFIG_SMP
  484. if (pr->id >= setup_max_cpus && pr->id != 0)
  485. return 0;
  486. #endif
  487. BUG_ON((pr->id >= nr_cpu_ids) || (pr->id < 0));
  488. /*
  489. * Buggy BIOS check
  490. * ACPI id of processors can be reported wrongly by the BIOS.
  491. * Don't trust it blindly
  492. */
  493. if (per_cpu(processor_device_array, pr->id) != NULL &&
  494. per_cpu(processor_device_array, pr->id) != device) {
  495. dev_warn(&device->dev,
  496. "BIOS reported wrong ACPI id %d for the processor\n",
  497. pr->id);
  498. result = -ENODEV;
  499. goto err_free_cpumask;
  500. }
  501. per_cpu(processor_device_array, pr->id) = device;
  502. per_cpu(processors, pr->id) = pr;
  503. dev = get_cpu_device(pr->id);
  504. if (sysfs_create_link(&device->dev.kobj, &dev->kobj, "sysdev")) {
  505. result = -EFAULT;
  506. goto err_clear_processor;
  507. }
  508. /*
  509. * Do not start hotplugged CPUs now, but when they
  510. * are onlined the first time
  511. */
  512. if (pr->flags.need_hotplug_init)
  513. return 0;
  514. result = acpi_processor_start(pr);
  515. if (result)
  516. goto err_remove_sysfs;
  517. return 0;
  518. err_remove_sysfs:
  519. sysfs_remove_link(&device->dev.kobj, "sysdev");
  520. err_clear_processor:
  521. /*
  522. * processor_device_array is not cleared to allow checks for buggy BIOS
  523. */
  524. per_cpu(processors, pr->id) = NULL;
  525. err_free_cpumask:
  526. free_cpumask_var(pr->throttling.shared_cpu_map);
  527. err_free_pr:
  528. kfree(pr);
  529. return result;
  530. }
  531. static int acpi_processor_remove(struct acpi_device *device)
  532. {
  533. struct acpi_processor *pr = NULL;
  534. if (!device || !acpi_driver_data(device))
  535. return -EINVAL;
  536. pr = acpi_driver_data(device);
  537. if (pr->id >= nr_cpu_ids)
  538. goto free;
  539. if (device->removal_type == ACPI_BUS_REMOVAL_EJECT) {
  540. if (acpi_processor_handle_eject(pr))
  541. return -EINVAL;
  542. }
  543. acpi_processor_power_exit(pr);
  544. sysfs_remove_link(&device->dev.kobj, "sysdev");
  545. if (pr->cdev) {
  546. sysfs_remove_link(&device->dev.kobj, "thermal_cooling");
  547. sysfs_remove_link(&pr->cdev->device.kobj, "device");
  548. thermal_cooling_device_unregister(pr->cdev);
  549. pr->cdev = NULL;
  550. }
  551. per_cpu(processors, pr->id) = NULL;
  552. per_cpu(processor_device_array, pr->id) = NULL;
  553. try_offline_node(cpu_to_node(pr->id));
  554. free:
  555. free_cpumask_var(pr->throttling.shared_cpu_map);
  556. kfree(pr);
  557. return 0;
  558. }
  559. #ifdef CONFIG_ACPI_HOTPLUG_CPU
  560. /****************************************************************************
  561. * Acpi processor hotplug support *
  562. ****************************************************************************/
  563. static int is_processor_present(acpi_handle handle)
  564. {
  565. acpi_status status;
  566. unsigned long long sta = 0;
  567. status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
  568. if (ACPI_SUCCESS(status) && (sta & ACPI_STA_DEVICE_PRESENT))
  569. return 1;
  570. /*
  571. * _STA is mandatory for a processor that supports hot plug
  572. */
  573. if (status == AE_NOT_FOUND)
  574. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  575. "Processor does not support hot plug\n"));
  576. else
  577. ACPI_EXCEPTION((AE_INFO, status,
  578. "Processor Device is not present"));
  579. return 0;
  580. }
  581. static void acpi_processor_hotplug_notify(acpi_handle handle,
  582. u32 event, void *data)
  583. {
  584. struct acpi_device *device = NULL;
  585. struct acpi_eject_event *ej_event = NULL;
  586. u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE; /* default */
  587. acpi_status status;
  588. int result;
  589. acpi_scan_lock_acquire();
  590. switch (event) {
  591. case ACPI_NOTIFY_BUS_CHECK:
  592. case ACPI_NOTIFY_DEVICE_CHECK:
  593. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  594. "Processor driver received %s event\n",
  595. (event == ACPI_NOTIFY_BUS_CHECK) ?
  596. "ACPI_NOTIFY_BUS_CHECK" : "ACPI_NOTIFY_DEVICE_CHECK"));
  597. if (!is_processor_present(handle))
  598. break;
  599. if (!acpi_bus_get_device(handle, &device))
  600. break;
  601. result = acpi_bus_scan(handle);
  602. if (result) {
  603. acpi_handle_err(handle, "Unable to add the device\n");
  604. break;
  605. }
  606. result = acpi_bus_get_device(handle, &device);
  607. if (result) {
  608. acpi_handle_err(handle, "Missing device object\n");
  609. break;
  610. }
  611. ost_code = ACPI_OST_SC_SUCCESS;
  612. break;
  613. case ACPI_NOTIFY_EJECT_REQUEST:
  614. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  615. "received ACPI_NOTIFY_EJECT_REQUEST\n"));
  616. if (acpi_bus_get_device(handle, &device)) {
  617. acpi_handle_err(handle,
  618. "Device don't exist, dropping EJECT\n");
  619. break;
  620. }
  621. if (!acpi_driver_data(device)) {
  622. acpi_handle_err(handle,
  623. "Driver data is NULL, dropping EJECT\n");
  624. break;
  625. }
  626. ej_event = kmalloc(sizeof(*ej_event), GFP_KERNEL);
  627. if (!ej_event) {
  628. acpi_handle_err(handle, "No memory, dropping EJECT\n");
  629. break;
  630. }
  631. get_device(&device->dev);
  632. ej_event->device = device;
  633. ej_event->event = ACPI_NOTIFY_EJECT_REQUEST;
  634. /* The eject is carried out asynchronously. */
  635. status = acpi_os_hotplug_execute(acpi_bus_hot_remove_device,
  636. ej_event);
  637. if (ACPI_FAILURE(status)) {
  638. put_device(&device->dev);
  639. kfree(ej_event);
  640. break;
  641. }
  642. goto out;
  643. default:
  644. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  645. "Unsupported event [0x%x]\n", event));
  646. /* non-hotplug event; possibly handled by other handler */
  647. goto out;
  648. }
  649. /* Inform firmware that the hotplug operation has completed */
  650. (void) acpi_evaluate_hotplug_ost(handle, event, ost_code, NULL);
  651. out:
  652. acpi_scan_lock_release();
  653. }
  654. static acpi_status is_processor_device(acpi_handle handle)
  655. {
  656. struct acpi_device_info *info;
  657. char *hid;
  658. acpi_status status;
  659. status = acpi_get_object_info(handle, &info);
  660. if (ACPI_FAILURE(status))
  661. return status;
  662. if (info->type == ACPI_TYPE_PROCESSOR) {
  663. kfree(info);
  664. return AE_OK; /* found a processor object */
  665. }
  666. if (!(info->valid & ACPI_VALID_HID)) {
  667. kfree(info);
  668. return AE_ERROR;
  669. }
  670. hid = info->hardware_id.string;
  671. if ((hid == NULL) || strcmp(hid, ACPI_PROCESSOR_DEVICE_HID)) {
  672. kfree(info);
  673. return AE_ERROR;
  674. }
  675. kfree(info);
  676. return AE_OK; /* found a processor device object */
  677. }
  678. static acpi_status
  679. processor_walk_namespace_cb(acpi_handle handle,
  680. u32 lvl, void *context, void **rv)
  681. {
  682. acpi_status status;
  683. int *action = context;
  684. status = is_processor_device(handle);
  685. if (ACPI_FAILURE(status))
  686. return AE_OK; /* not a processor; continue to walk */
  687. switch (*action) {
  688. case INSTALL_NOTIFY_HANDLER:
  689. acpi_install_notify_handler(handle,
  690. ACPI_SYSTEM_NOTIFY,
  691. acpi_processor_hotplug_notify,
  692. NULL);
  693. break;
  694. case UNINSTALL_NOTIFY_HANDLER:
  695. acpi_remove_notify_handler(handle,
  696. ACPI_SYSTEM_NOTIFY,
  697. acpi_processor_hotplug_notify);
  698. break;
  699. default:
  700. break;
  701. }
  702. /* found a processor; skip walking underneath */
  703. return AE_CTRL_DEPTH;
  704. }
  705. static acpi_status acpi_processor_hotadd_init(struct acpi_processor *pr)
  706. {
  707. acpi_handle handle = pr->handle;
  708. if (!is_processor_present(handle)) {
  709. return AE_ERROR;
  710. }
  711. if (acpi_map_lsapic(handle, &pr->id))
  712. return AE_ERROR;
  713. if (arch_register_cpu(pr->id)) {
  714. acpi_unmap_lsapic(pr->id);
  715. return AE_ERROR;
  716. }
  717. /* CPU got hot-plugged, but cpu_data is not initialized yet
  718. * Set flag to delay cpu_idle/throttling initialization
  719. * in:
  720. * acpi_processor_add()
  721. * acpi_processor_get_info()
  722. * and do it when the CPU gets online the first time
  723. * TBD: Cleanup above functions and try to do this more elegant.
  724. */
  725. pr_info("CPU %d got hotplugged\n", pr->id);
  726. pr->flags.need_hotplug_init = 1;
  727. return AE_OK;
  728. }
  729. static int acpi_processor_handle_eject(struct acpi_processor *pr)
  730. {
  731. if (cpu_online(pr->id))
  732. cpu_down(pr->id);
  733. get_online_cpus();
  734. /*
  735. * The cpu might become online again at this point. So we check whether
  736. * the cpu has been onlined or not. If the cpu became online, it means
  737. * that someone wants to use the cpu. So acpi_processor_handle_eject()
  738. * returns -EAGAIN.
  739. */
  740. if (unlikely(cpu_online(pr->id))) {
  741. put_online_cpus();
  742. pr_warn("Failed to remove CPU %d, because other task "
  743. "brought the CPU back online\n", pr->id);
  744. return -EAGAIN;
  745. }
  746. arch_unregister_cpu(pr->id);
  747. acpi_unmap_lsapic(pr->id);
  748. put_online_cpus();
  749. return (0);
  750. }
  751. #else
  752. static acpi_status acpi_processor_hotadd_init(struct acpi_processor *pr)
  753. {
  754. return AE_ERROR;
  755. }
  756. static int acpi_processor_handle_eject(struct acpi_processor *pr)
  757. {
  758. return (-EINVAL);
  759. }
  760. #endif
  761. static
  762. void acpi_processor_install_hotplug_notify(void)
  763. {
  764. #ifdef CONFIG_ACPI_HOTPLUG_CPU
  765. int action = INSTALL_NOTIFY_HANDLER;
  766. acpi_walk_namespace(ACPI_TYPE_ANY,
  767. ACPI_ROOT_OBJECT,
  768. ACPI_UINT32_MAX,
  769. processor_walk_namespace_cb, NULL, &action, NULL);
  770. #endif
  771. register_hotcpu_notifier(&acpi_cpu_notifier);
  772. }
  773. static
  774. void acpi_processor_uninstall_hotplug_notify(void)
  775. {
  776. #ifdef CONFIG_ACPI_HOTPLUG_CPU
  777. int action = UNINSTALL_NOTIFY_HANDLER;
  778. acpi_walk_namespace(ACPI_TYPE_ANY,
  779. ACPI_ROOT_OBJECT,
  780. ACPI_UINT32_MAX,
  781. processor_walk_namespace_cb, NULL, &action, NULL);
  782. #endif
  783. unregister_hotcpu_notifier(&acpi_cpu_notifier);
  784. }
  785. /*
  786. * We keep the driver loaded even when ACPI is not running.
  787. * This is needed for the powernow-k8 driver, that works even without
  788. * ACPI, but needs symbols from this driver
  789. */
  790. static int __init acpi_processor_init(void)
  791. {
  792. int result = 0;
  793. if (acpi_disabled)
  794. return 0;
  795. result = acpi_bus_register_driver(&acpi_processor_driver);
  796. if (result < 0)
  797. return result;
  798. acpi_processor_install_hotplug_notify();
  799. acpi_thermal_cpufreq_init();
  800. acpi_processor_ppc_init();
  801. acpi_processor_throttling_init();
  802. return 0;
  803. }
  804. static void __exit acpi_processor_exit(void)
  805. {
  806. if (acpi_disabled)
  807. return;
  808. acpi_processor_ppc_exit();
  809. acpi_thermal_cpufreq_exit();
  810. acpi_processor_uninstall_hotplug_notify();
  811. acpi_bus_unregister_driver(&acpi_processor_driver);
  812. return;
  813. }
  814. module_init(acpi_processor_init);
  815. module_exit(acpi_processor_exit);
  816. MODULE_ALIAS("processor");