processor_driver.c 24 KB

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