processor_driver.c 25 KB

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