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