processor_core.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228
  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 <asm/io.h>
  48. #include <asm/system.h>
  49. #include <asm/cpu.h>
  50. #include <asm/delay.h>
  51. #include <asm/uaccess.h>
  52. #include <asm/processor.h>
  53. #include <asm/smp.h>
  54. #include <asm/acpi.h>
  55. #include <acpi/acpi_bus.h>
  56. #include <acpi/acpi_drivers.h>
  57. #include <acpi/processor.h>
  58. #define PREFIX "ACPI: "
  59. #define ACPI_PROCESSOR_CLASS "processor"
  60. #define ACPI_PROCESSOR_DEVICE_NAME "Processor"
  61. #define ACPI_PROCESSOR_FILE_INFO "info"
  62. #define ACPI_PROCESSOR_FILE_THROTTLING "throttling"
  63. #define ACPI_PROCESSOR_FILE_LIMIT "limit"
  64. #define ACPI_PROCESSOR_NOTIFY_PERFORMANCE 0x80
  65. #define ACPI_PROCESSOR_NOTIFY_POWER 0x81
  66. #define ACPI_PROCESSOR_NOTIFY_THROTTLING 0x82
  67. #define ACPI_PROCESSOR_LIMIT_USER 0
  68. #define ACPI_PROCESSOR_LIMIT_THERMAL 1
  69. #define _COMPONENT ACPI_PROCESSOR_COMPONENT
  70. ACPI_MODULE_NAME("processor_core");
  71. MODULE_AUTHOR("Paul Diefenbaugh");
  72. MODULE_DESCRIPTION("ACPI Processor Driver");
  73. MODULE_LICENSE("GPL");
  74. static int acpi_processor_add(struct acpi_device *device);
  75. static int acpi_processor_start(struct acpi_device *device);
  76. static int acpi_processor_remove(struct acpi_device *device, int type);
  77. static int acpi_processor_info_open_fs(struct inode *inode, struct file *file);
  78. static void acpi_processor_notify(struct acpi_device *device, u32 event);
  79. static acpi_status acpi_processor_hotadd_init(acpi_handle handle, int *p_cpu);
  80. static int acpi_processor_handle_eject(struct acpi_processor *pr);
  81. static const struct acpi_device_id processor_device_ids[] = {
  82. {ACPI_PROCESSOR_OBJECT_HID, 0},
  83. {"ACPI0007", 0},
  84. {"", 0},
  85. };
  86. MODULE_DEVICE_TABLE(acpi, processor_device_ids);
  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. .start = acpi_processor_start,
  95. .suspend = acpi_processor_suspend,
  96. .resume = acpi_processor_resume,
  97. .notify = acpi_processor_notify,
  98. },
  99. };
  100. #define INSTALL_NOTIFY_HANDLER 1
  101. #define UNINSTALL_NOTIFY_HANDLER 2
  102. static const struct file_operations acpi_processor_info_fops = {
  103. .owner = THIS_MODULE,
  104. .open = acpi_processor_info_open_fs,
  105. .read = seq_read,
  106. .llseek = seq_lseek,
  107. .release = single_release,
  108. };
  109. DEFINE_PER_CPU(struct acpi_processor *, processors);
  110. struct acpi_processor_errata errata __read_mostly;
  111. static int set_no_mwait(const struct dmi_system_id *id)
  112. {
  113. printk(KERN_NOTICE PREFIX "%s detected - "
  114. "disabling mwait for CPU C-states\n", id->ident);
  115. idle_nomwait = 1;
  116. return 0;
  117. }
  118. static struct dmi_system_id __cpuinitdata processor_idle_dmi_table[] = {
  119. {
  120. set_no_mwait, "IFL91 board", {
  121. DMI_MATCH(DMI_BIOS_VENDOR, "COMPAL"),
  122. DMI_MATCH(DMI_SYS_VENDOR, "ZEPTO"),
  123. DMI_MATCH(DMI_PRODUCT_VERSION, "3215W"),
  124. DMI_MATCH(DMI_BOARD_NAME, "IFL91") }, NULL},
  125. {
  126. set_no_mwait, "Extensa 5220", {
  127. DMI_MATCH(DMI_BIOS_VENDOR, "Phoenix Technologies LTD"),
  128. DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
  129. DMI_MATCH(DMI_PRODUCT_VERSION, "0100"),
  130. DMI_MATCH(DMI_BOARD_NAME, "Columbia") }, NULL},
  131. {},
  132. };
  133. /* --------------------------------------------------------------------------
  134. Errata Handling
  135. -------------------------------------------------------------------------- */
  136. static int acpi_processor_errata_piix4(struct pci_dev *dev)
  137. {
  138. u8 value1 = 0;
  139. u8 value2 = 0;
  140. if (!dev)
  141. return -EINVAL;
  142. /*
  143. * Note that 'dev' references the PIIX4 ACPI Controller.
  144. */
  145. switch (dev->revision) {
  146. case 0:
  147. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found PIIX4 A-step\n"));
  148. break;
  149. case 1:
  150. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found PIIX4 B-step\n"));
  151. break;
  152. case 2:
  153. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found PIIX4E\n"));
  154. break;
  155. case 3:
  156. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found PIIX4M\n"));
  157. break;
  158. default:
  159. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found unknown PIIX4\n"));
  160. break;
  161. }
  162. switch (dev->revision) {
  163. case 0: /* PIIX4 A-step */
  164. case 1: /* PIIX4 B-step */
  165. /*
  166. * See specification changes #13 ("Manual Throttle Duty Cycle")
  167. * and #14 ("Enabling and Disabling Manual Throttle"), plus
  168. * erratum #5 ("STPCLK# Deassertion Time") from the January
  169. * 2002 PIIX4 specification update. Applies to only older
  170. * PIIX4 models.
  171. */
  172. errata.piix4.throttle = 1;
  173. case 2: /* PIIX4E */
  174. case 3: /* PIIX4M */
  175. /*
  176. * See erratum #18 ("C3 Power State/BMIDE and Type-F DMA
  177. * Livelock") from the January 2002 PIIX4 specification update.
  178. * Applies to all PIIX4 models.
  179. */
  180. /*
  181. * BM-IDE
  182. * ------
  183. * Find the PIIX4 IDE Controller and get the Bus Master IDE
  184. * Status register address. We'll use this later to read
  185. * each IDE controller's DMA status to make sure we catch all
  186. * DMA activity.
  187. */
  188. dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
  189. PCI_DEVICE_ID_INTEL_82371AB,
  190. PCI_ANY_ID, PCI_ANY_ID, NULL);
  191. if (dev) {
  192. errata.piix4.bmisx = pci_resource_start(dev, 4);
  193. pci_dev_put(dev);
  194. }
  195. /*
  196. * Type-F DMA
  197. * ----------
  198. * Find the PIIX4 ISA Controller and read the Motherboard
  199. * DMA controller's status to see if Type-F (Fast) DMA mode
  200. * is enabled (bit 7) on either channel. Note that we'll
  201. * disable C3 support if this is enabled, as some legacy
  202. * devices won't operate well if fast DMA is disabled.
  203. */
  204. dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
  205. PCI_DEVICE_ID_INTEL_82371AB_0,
  206. PCI_ANY_ID, PCI_ANY_ID, NULL);
  207. if (dev) {
  208. pci_read_config_byte(dev, 0x76, &value1);
  209. pci_read_config_byte(dev, 0x77, &value2);
  210. if ((value1 & 0x80) || (value2 & 0x80))
  211. errata.piix4.fdma = 1;
  212. pci_dev_put(dev);
  213. }
  214. break;
  215. }
  216. if (errata.piix4.bmisx)
  217. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  218. "Bus master activity detection (BM-IDE) erratum enabled\n"));
  219. if (errata.piix4.fdma)
  220. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  221. "Type-F DMA livelock erratum (C3 disabled)\n"));
  222. return 0;
  223. }
  224. static int acpi_processor_errata(struct acpi_processor *pr)
  225. {
  226. int result = 0;
  227. struct pci_dev *dev = NULL;
  228. if (!pr)
  229. return -EINVAL;
  230. /*
  231. * PIIX4
  232. */
  233. dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
  234. PCI_DEVICE_ID_INTEL_82371AB_3, PCI_ANY_ID,
  235. PCI_ANY_ID, NULL);
  236. if (dev) {
  237. result = acpi_processor_errata_piix4(dev);
  238. pci_dev_put(dev);
  239. }
  240. return result;
  241. }
  242. /* --------------------------------------------------------------------------
  243. Common ACPI processor functions
  244. -------------------------------------------------------------------------- */
  245. /*
  246. * _PDC is required for a BIOS-OS handshake for most of the newer
  247. * ACPI processor features.
  248. */
  249. static int acpi_processor_set_pdc(struct acpi_processor *pr)
  250. {
  251. struct acpi_object_list *pdc_in = pr->pdc;
  252. acpi_status status = AE_OK;
  253. if (!pdc_in)
  254. return status;
  255. if (idle_nomwait) {
  256. /*
  257. * If mwait is disabled for CPU C-states, the C2C3_FFH access
  258. * mode will be disabled in the parameter of _PDC object.
  259. * Of course C1_FFH access mode will also be disabled.
  260. */
  261. union acpi_object *obj;
  262. u32 *buffer = NULL;
  263. obj = pdc_in->pointer;
  264. buffer = (u32 *)(obj->buffer.pointer);
  265. buffer[2] &= ~(ACPI_PDC_C_C2C3_FFH | ACPI_PDC_C_C1_FFH);
  266. }
  267. status = acpi_evaluate_object(pr->handle, "_PDC", pdc_in, NULL);
  268. if (ACPI_FAILURE(status))
  269. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  270. "Could not evaluate _PDC, using legacy perf. control...\n"));
  271. return status;
  272. }
  273. /* --------------------------------------------------------------------------
  274. FS Interface (/proc)
  275. -------------------------------------------------------------------------- */
  276. static struct proc_dir_entry *acpi_processor_dir = NULL;
  277. static int acpi_processor_info_seq_show(struct seq_file *seq, void *offset)
  278. {
  279. struct acpi_processor *pr = seq->private;
  280. if (!pr)
  281. goto end;
  282. seq_printf(seq, "processor id: %d\n"
  283. "acpi id: %d\n"
  284. "bus mastering control: %s\n"
  285. "power management: %s\n"
  286. "throttling control: %s\n"
  287. "limit interface: %s\n",
  288. pr->id,
  289. pr->acpi_id,
  290. pr->flags.bm_control ? "yes" : "no",
  291. pr->flags.power ? "yes" : "no",
  292. pr->flags.throttling ? "yes" : "no",
  293. pr->flags.limit ? "yes" : "no");
  294. end:
  295. return 0;
  296. }
  297. static int acpi_processor_info_open_fs(struct inode *inode, struct file *file)
  298. {
  299. return single_open(file, acpi_processor_info_seq_show,
  300. PDE(inode)->data);
  301. }
  302. static int acpi_processor_add_fs(struct acpi_device *device)
  303. {
  304. struct proc_dir_entry *entry = NULL;
  305. if (!acpi_device_dir(device)) {
  306. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  307. acpi_processor_dir);
  308. if (!acpi_device_dir(device))
  309. return -ENODEV;
  310. }
  311. /* 'info' [R] */
  312. entry = proc_create_data(ACPI_PROCESSOR_FILE_INFO,
  313. S_IRUGO, acpi_device_dir(device),
  314. &acpi_processor_info_fops,
  315. acpi_driver_data(device));
  316. if (!entry)
  317. return -EIO;
  318. /* 'throttling' [R/W] */
  319. entry = proc_create_data(ACPI_PROCESSOR_FILE_THROTTLING,
  320. S_IFREG | S_IRUGO | S_IWUSR,
  321. acpi_device_dir(device),
  322. &acpi_processor_throttling_fops,
  323. acpi_driver_data(device));
  324. if (!entry)
  325. return -EIO;
  326. /* 'limit' [R/W] */
  327. entry = proc_create_data(ACPI_PROCESSOR_FILE_LIMIT,
  328. S_IFREG | S_IRUGO | S_IWUSR,
  329. acpi_device_dir(device),
  330. &acpi_processor_limit_fops,
  331. acpi_driver_data(device));
  332. if (!entry)
  333. return -EIO;
  334. return 0;
  335. }
  336. static int acpi_processor_remove_fs(struct acpi_device *device)
  337. {
  338. if (acpi_device_dir(device)) {
  339. remove_proc_entry(ACPI_PROCESSOR_FILE_INFO,
  340. acpi_device_dir(device));
  341. remove_proc_entry(ACPI_PROCESSOR_FILE_THROTTLING,
  342. acpi_device_dir(device));
  343. remove_proc_entry(ACPI_PROCESSOR_FILE_LIMIT,
  344. acpi_device_dir(device));
  345. remove_proc_entry(acpi_device_bid(device), acpi_processor_dir);
  346. acpi_device_dir(device) = NULL;
  347. }
  348. return 0;
  349. }
  350. /* Use the acpiid in MADT to map cpus in case of SMP */
  351. #ifndef CONFIG_SMP
  352. static int get_cpu_id(acpi_handle handle, int type, u32 acpi_id) { return -1; }
  353. #else
  354. static struct acpi_table_madt *madt;
  355. static int map_lapic_id(struct acpi_subtable_header *entry,
  356. u32 acpi_id, int *apic_id)
  357. {
  358. struct acpi_madt_local_apic *lapic =
  359. (struct acpi_madt_local_apic *)entry;
  360. if ((lapic->lapic_flags & ACPI_MADT_ENABLED) &&
  361. lapic->processor_id == acpi_id) {
  362. *apic_id = lapic->id;
  363. return 1;
  364. }
  365. return 0;
  366. }
  367. static int map_x2apic_id(struct acpi_subtable_header *entry,
  368. int device_declaration, u32 acpi_id, int *apic_id)
  369. {
  370. struct acpi_madt_local_x2apic *apic =
  371. (struct acpi_madt_local_x2apic *)entry;
  372. u32 tmp = apic->local_apic_id;
  373. /* Only check enabled APICs*/
  374. if (!(apic->lapic_flags & ACPI_MADT_ENABLED))
  375. return 0;
  376. /* Device statement declaration type */
  377. if (device_declaration) {
  378. if (apic->uid == acpi_id)
  379. goto found;
  380. }
  381. return 0;
  382. found:
  383. *apic_id = tmp;
  384. return 1;
  385. }
  386. static int map_lsapic_id(struct acpi_subtable_header *entry,
  387. int device_declaration, u32 acpi_id, int *apic_id)
  388. {
  389. struct acpi_madt_local_sapic *lsapic =
  390. (struct acpi_madt_local_sapic *)entry;
  391. u32 tmp = (lsapic->id << 8) | lsapic->eid;
  392. /* Only check enabled APICs*/
  393. if (!(lsapic->lapic_flags & ACPI_MADT_ENABLED))
  394. return 0;
  395. /* Device statement declaration type */
  396. if (device_declaration) {
  397. if (entry->length < 16)
  398. printk(KERN_ERR PREFIX
  399. "Invalid LSAPIC with Device type processor (SAPIC ID %#x)\n",
  400. tmp);
  401. else if (lsapic->uid == acpi_id)
  402. goto found;
  403. /* Processor statement declaration type */
  404. } else if (lsapic->processor_id == acpi_id)
  405. goto found;
  406. return 0;
  407. found:
  408. *apic_id = tmp;
  409. return 1;
  410. }
  411. static int map_madt_entry(int type, u32 acpi_id)
  412. {
  413. unsigned long madt_end, entry;
  414. int apic_id = -1;
  415. if (!madt)
  416. return apic_id;
  417. entry = (unsigned long)madt;
  418. madt_end = entry + madt->header.length;
  419. /* Parse all entries looking for a match. */
  420. entry += sizeof(struct acpi_table_madt);
  421. while (entry + sizeof(struct acpi_subtable_header) < madt_end) {
  422. struct acpi_subtable_header *header =
  423. (struct acpi_subtable_header *)entry;
  424. if (header->type == ACPI_MADT_TYPE_LOCAL_APIC) {
  425. if (map_lapic_id(header, acpi_id, &apic_id))
  426. break;
  427. } else if (header->type == ACPI_MADT_TYPE_LOCAL_X2APIC) {
  428. if (map_x2apic_id(header, type, acpi_id, &apic_id))
  429. break;
  430. } else if (header->type == ACPI_MADT_TYPE_LOCAL_SAPIC) {
  431. if (map_lsapic_id(header, type, acpi_id, &apic_id))
  432. break;
  433. }
  434. entry += header->length;
  435. }
  436. return apic_id;
  437. }
  438. static int map_mat_entry(acpi_handle handle, int type, u32 acpi_id)
  439. {
  440. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  441. union acpi_object *obj;
  442. struct acpi_subtable_header *header;
  443. int apic_id = -1;
  444. if (ACPI_FAILURE(acpi_evaluate_object(handle, "_MAT", NULL, &buffer)))
  445. goto exit;
  446. if (!buffer.length || !buffer.pointer)
  447. goto exit;
  448. obj = buffer.pointer;
  449. if (obj->type != ACPI_TYPE_BUFFER ||
  450. obj->buffer.length < sizeof(struct acpi_subtable_header)) {
  451. goto exit;
  452. }
  453. header = (struct acpi_subtable_header *)obj->buffer.pointer;
  454. if (header->type == ACPI_MADT_TYPE_LOCAL_APIC) {
  455. map_lapic_id(header, acpi_id, &apic_id);
  456. } else if (header->type == ACPI_MADT_TYPE_LOCAL_SAPIC) {
  457. map_lsapic_id(header, type, acpi_id, &apic_id);
  458. }
  459. exit:
  460. if (buffer.pointer)
  461. kfree(buffer.pointer);
  462. return apic_id;
  463. }
  464. static int get_cpu_id(acpi_handle handle, int type, u32 acpi_id)
  465. {
  466. int i;
  467. int apic_id = -1;
  468. apic_id = map_mat_entry(handle, type, acpi_id);
  469. if (apic_id == -1)
  470. apic_id = map_madt_entry(type, acpi_id);
  471. if (apic_id == -1)
  472. return apic_id;
  473. for_each_possible_cpu(i) {
  474. if (cpu_physical_id(i) == apic_id)
  475. return i;
  476. }
  477. return -1;
  478. }
  479. #endif
  480. /* --------------------------------------------------------------------------
  481. Driver Interface
  482. -------------------------------------------------------------------------- */
  483. static int acpi_processor_get_info(struct acpi_device *device)
  484. {
  485. acpi_status status = 0;
  486. union acpi_object object = { 0 };
  487. struct acpi_buffer buffer = { sizeof(union acpi_object), &object };
  488. struct acpi_processor *pr;
  489. int cpu_index, device_declaration = 0;
  490. static int cpu0_initialized;
  491. pr = acpi_driver_data(device);
  492. if (!pr)
  493. return -EINVAL;
  494. if (num_online_cpus() > 1)
  495. errata.smp = TRUE;
  496. acpi_processor_errata(pr);
  497. /*
  498. * Check to see if we have bus mastering arbitration control. This
  499. * is required for proper C3 usage (to maintain cache coherency).
  500. */
  501. if (acpi_gbl_FADT.pm2_control_block && acpi_gbl_FADT.pm2_control_length) {
  502. pr->flags.bm_control = 1;
  503. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  504. "Bus mastering arbitration control present\n"));
  505. } else
  506. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  507. "No bus mastering arbitration control\n"));
  508. if (!strcmp(acpi_device_hid(device), ACPI_PROCESSOR_OBJECT_HID)) {
  509. /* Declared with "Processor" statement; match ProcessorID */
  510. status = acpi_evaluate_object(pr->handle, NULL, NULL, &buffer);
  511. if (ACPI_FAILURE(status)) {
  512. printk(KERN_ERR PREFIX "Evaluating processor object\n");
  513. return -ENODEV;
  514. }
  515. /*
  516. * TBD: Synch processor ID (via LAPIC/LSAPIC structures) on SMP.
  517. * >>> 'acpi_get_processor_id(acpi_id, &id)' in
  518. * arch/xxx/acpi.c
  519. */
  520. pr->acpi_id = object.processor.proc_id;
  521. } else {
  522. /*
  523. * Declared with "Device" statement; match _UID.
  524. * Note that we don't handle string _UIDs yet.
  525. */
  526. unsigned long long value;
  527. status = acpi_evaluate_integer(pr->handle, METHOD_NAME__UID,
  528. NULL, &value);
  529. if (ACPI_FAILURE(status)) {
  530. printk(KERN_ERR PREFIX
  531. "Evaluating processor _UID [%#x]\n", status);
  532. return -ENODEV;
  533. }
  534. device_declaration = 1;
  535. pr->acpi_id = value;
  536. }
  537. cpu_index = get_cpu_id(pr->handle, device_declaration, pr->acpi_id);
  538. /* Handle UP system running SMP kernel, with no LAPIC in MADT */
  539. if (!cpu0_initialized && (cpu_index == -1) &&
  540. (num_online_cpus() == 1)) {
  541. cpu_index = 0;
  542. }
  543. cpu0_initialized = 1;
  544. pr->id = cpu_index;
  545. /*
  546. * Extra Processor objects may be enumerated on MP systems with
  547. * less than the max # of CPUs. They should be ignored _iff
  548. * they are physically not present.
  549. */
  550. if (pr->id == -1) {
  551. if (ACPI_FAILURE
  552. (acpi_processor_hotadd_init(pr->handle, &pr->id))) {
  553. return -ENODEV;
  554. }
  555. }
  556. /*
  557. * On some boxes several processors use the same processor bus id.
  558. * But they are located in different scope. For example:
  559. * \_SB.SCK0.CPU0
  560. * \_SB.SCK1.CPU0
  561. * Rename the processor device bus id. And the new bus id will be
  562. * generated as the following format:
  563. * CPU+CPU ID.
  564. */
  565. sprintf(acpi_device_bid(device), "CPU%X", pr->id);
  566. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Processor [%d:%d]\n", pr->id,
  567. pr->acpi_id));
  568. if (!object.processor.pblk_address)
  569. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No PBLK (NULL address)\n"));
  570. else if (object.processor.pblk_length != 6)
  571. printk(KERN_ERR PREFIX "Invalid PBLK length [%d]\n",
  572. object.processor.pblk_length);
  573. else {
  574. pr->throttling.address = object.processor.pblk_address;
  575. pr->throttling.duty_offset = acpi_gbl_FADT.duty_offset;
  576. pr->throttling.duty_width = acpi_gbl_FADT.duty_width;
  577. pr->pblk = object.processor.pblk_address;
  578. /*
  579. * We don't care about error returns - we just try to mark
  580. * these reserved so that nobody else is confused into thinking
  581. * that this region might be unused..
  582. *
  583. * (In particular, allocating the IO range for Cardbus)
  584. */
  585. request_region(pr->throttling.address, 6, "ACPI CPU throttle");
  586. }
  587. /*
  588. * If ACPI describes a slot number for this CPU, we can use it
  589. * ensure we get the right value in the "physical id" field
  590. * of /proc/cpuinfo
  591. */
  592. status = acpi_evaluate_object(pr->handle, "_SUN", NULL, &buffer);
  593. if (ACPI_SUCCESS(status))
  594. arch_fix_phys_package_id(pr->id, object.integer.value);
  595. return 0;
  596. }
  597. static DEFINE_PER_CPU(void *, processor_device_array);
  598. static int __cpuinit acpi_processor_start(struct acpi_device *device)
  599. {
  600. int result = 0;
  601. struct acpi_processor *pr;
  602. struct sys_device *sysdev;
  603. pr = acpi_driver_data(device);
  604. result = acpi_processor_get_info(device);
  605. if (result) {
  606. /* Processor is physically not present */
  607. return 0;
  608. }
  609. BUG_ON((pr->id >= nr_cpu_ids) || (pr->id < 0));
  610. /*
  611. * Buggy BIOS check
  612. * ACPI id of processors can be reported wrongly by the BIOS.
  613. * Don't trust it blindly
  614. */
  615. if (per_cpu(processor_device_array, pr->id) != NULL &&
  616. per_cpu(processor_device_array, pr->id) != device) {
  617. printk(KERN_WARNING "BIOS reported wrong ACPI id "
  618. "for the processor\n");
  619. return -ENODEV;
  620. }
  621. per_cpu(processor_device_array, pr->id) = device;
  622. per_cpu(processors, pr->id) = pr;
  623. result = acpi_processor_add_fs(device);
  624. if (result)
  625. goto end;
  626. sysdev = get_cpu_sysdev(pr->id);
  627. if (sysfs_create_link(&device->dev.kobj, &sysdev->kobj, "sysdev"))
  628. return -EFAULT;
  629. /* _PDC call should be done before doing anything else (if reqd.). */
  630. arch_acpi_processor_init_pdc(pr);
  631. acpi_processor_set_pdc(pr);
  632. arch_acpi_processor_cleanup_pdc(pr);
  633. #ifdef CONFIG_CPU_FREQ
  634. acpi_processor_ppc_has_changed(pr);
  635. #endif
  636. acpi_processor_get_throttling_info(pr);
  637. acpi_processor_get_limit_info(pr);
  638. acpi_processor_power_init(pr, device);
  639. pr->cdev = thermal_cooling_device_register("Processor", device,
  640. &processor_cooling_ops);
  641. if (IS_ERR(pr->cdev)) {
  642. result = PTR_ERR(pr->cdev);
  643. goto end;
  644. }
  645. dev_info(&device->dev, "registered as cooling_device%d\n",
  646. pr->cdev->id);
  647. result = sysfs_create_link(&device->dev.kobj,
  648. &pr->cdev->device.kobj,
  649. "thermal_cooling");
  650. if (result)
  651. printk(KERN_ERR PREFIX "Create sysfs link\n");
  652. result = sysfs_create_link(&pr->cdev->device.kobj,
  653. &device->dev.kobj,
  654. "device");
  655. if (result)
  656. printk(KERN_ERR PREFIX "Create sysfs link\n");
  657. if (pr->flags.throttling) {
  658. printk(KERN_INFO PREFIX "%s [%s] (supports",
  659. acpi_device_name(device), acpi_device_bid(device));
  660. printk(" %d throttling states", pr->throttling.state_count);
  661. printk(")\n");
  662. }
  663. end:
  664. return result;
  665. }
  666. static void acpi_processor_notify(struct acpi_device *device, u32 event)
  667. {
  668. struct acpi_processor *pr = acpi_driver_data(device);
  669. int saved;
  670. if (!pr)
  671. return;
  672. switch (event) {
  673. case ACPI_PROCESSOR_NOTIFY_PERFORMANCE:
  674. saved = pr->performance_platform_limit;
  675. acpi_processor_ppc_has_changed(pr);
  676. if (saved == pr->performance_platform_limit)
  677. break;
  678. acpi_bus_generate_proc_event(device, event,
  679. pr->performance_platform_limit);
  680. acpi_bus_generate_netlink_event(device->pnp.device_class,
  681. dev_name(&device->dev), event,
  682. pr->performance_platform_limit);
  683. break;
  684. case ACPI_PROCESSOR_NOTIFY_POWER:
  685. acpi_processor_cst_has_changed(pr);
  686. acpi_bus_generate_proc_event(device, event, 0);
  687. acpi_bus_generate_netlink_event(device->pnp.device_class,
  688. dev_name(&device->dev), event, 0);
  689. break;
  690. case ACPI_PROCESSOR_NOTIFY_THROTTLING:
  691. acpi_processor_tstate_has_changed(pr);
  692. acpi_bus_generate_proc_event(device, event, 0);
  693. acpi_bus_generate_netlink_event(device->pnp.device_class,
  694. dev_name(&device->dev), event, 0);
  695. default:
  696. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  697. "Unsupported event [0x%x]\n", event));
  698. break;
  699. }
  700. return;
  701. }
  702. static int acpi_cpu_soft_notify(struct notifier_block *nfb,
  703. unsigned long action, void *hcpu)
  704. {
  705. unsigned int cpu = (unsigned long)hcpu;
  706. struct acpi_processor *pr = per_cpu(processors, cpu);
  707. if (action == CPU_ONLINE && pr) {
  708. acpi_processor_ppc_has_changed(pr);
  709. acpi_processor_cst_has_changed(pr);
  710. acpi_processor_tstate_has_changed(pr);
  711. }
  712. return NOTIFY_OK;
  713. }
  714. static struct notifier_block acpi_cpu_notifier =
  715. {
  716. .notifier_call = acpi_cpu_soft_notify,
  717. };
  718. static int acpi_processor_add(struct acpi_device *device)
  719. {
  720. struct acpi_processor *pr = NULL;
  721. if (!device)
  722. return -EINVAL;
  723. pr = kzalloc(sizeof(struct acpi_processor), GFP_KERNEL);
  724. if (!pr)
  725. return -ENOMEM;
  726. if (!zalloc_cpumask_var(&pr->throttling.shared_cpu_map, GFP_KERNEL)) {
  727. kfree(pr);
  728. return -ENOMEM;
  729. }
  730. pr->handle = device->handle;
  731. strcpy(acpi_device_name(device), ACPI_PROCESSOR_DEVICE_NAME);
  732. strcpy(acpi_device_class(device), ACPI_PROCESSOR_CLASS);
  733. device->driver_data = pr;
  734. return 0;
  735. }
  736. static int acpi_processor_remove(struct acpi_device *device, int type)
  737. {
  738. struct acpi_processor *pr = NULL;
  739. if (!device || !acpi_driver_data(device))
  740. return -EINVAL;
  741. pr = acpi_driver_data(device);
  742. if (pr->id >= nr_cpu_ids)
  743. goto free;
  744. if (type == ACPI_BUS_REMOVAL_EJECT) {
  745. if (acpi_processor_handle_eject(pr))
  746. return -EINVAL;
  747. }
  748. acpi_processor_power_exit(pr, device);
  749. sysfs_remove_link(&device->dev.kobj, "sysdev");
  750. acpi_processor_remove_fs(device);
  751. if (pr->cdev) {
  752. sysfs_remove_link(&device->dev.kobj, "thermal_cooling");
  753. sysfs_remove_link(&pr->cdev->device.kobj, "device");
  754. thermal_cooling_device_unregister(pr->cdev);
  755. pr->cdev = NULL;
  756. }
  757. per_cpu(processors, pr->id) = NULL;
  758. per_cpu(processor_device_array, pr->id) = NULL;
  759. free:
  760. free_cpumask_var(pr->throttling.shared_cpu_map);
  761. kfree(pr);
  762. return 0;
  763. }
  764. #ifdef CONFIG_ACPI_HOTPLUG_CPU
  765. /****************************************************************************
  766. * Acpi processor hotplug support *
  767. ****************************************************************************/
  768. static int is_processor_present(acpi_handle handle)
  769. {
  770. acpi_status status;
  771. unsigned long long sta = 0;
  772. status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
  773. if (ACPI_SUCCESS(status) && (sta & ACPI_STA_DEVICE_PRESENT))
  774. return 1;
  775. /*
  776. * _STA is mandatory for a processor that supports hot plug
  777. */
  778. if (status == AE_NOT_FOUND)
  779. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  780. "Processor does not support hot plug\n"));
  781. else
  782. ACPI_EXCEPTION((AE_INFO, status,
  783. "Processor Device is not present"));
  784. return 0;
  785. }
  786. static
  787. int acpi_processor_device_add(acpi_handle handle, struct acpi_device **device)
  788. {
  789. acpi_handle phandle;
  790. struct acpi_device *pdev;
  791. struct acpi_processor *pr;
  792. if (acpi_get_parent(handle, &phandle)) {
  793. return -ENODEV;
  794. }
  795. if (acpi_bus_get_device(phandle, &pdev)) {
  796. return -ENODEV;
  797. }
  798. if (acpi_bus_add(device, pdev, handle, ACPI_BUS_TYPE_PROCESSOR)) {
  799. return -ENODEV;
  800. }
  801. acpi_bus_start(*device);
  802. pr = acpi_driver_data(*device);
  803. if (!pr)
  804. return -ENODEV;
  805. if ((pr->id >= 0) && (pr->id < nr_cpu_ids)) {
  806. kobject_uevent(&(*device)->dev.kobj, KOBJ_ONLINE);
  807. }
  808. return 0;
  809. }
  810. static void __ref acpi_processor_hotplug_notify(acpi_handle handle,
  811. u32 event, void *data)
  812. {
  813. struct acpi_processor *pr;
  814. struct acpi_device *device = NULL;
  815. int result;
  816. switch (event) {
  817. case ACPI_NOTIFY_BUS_CHECK:
  818. case ACPI_NOTIFY_DEVICE_CHECK:
  819. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  820. "Processor driver received %s event\n",
  821. (event == ACPI_NOTIFY_BUS_CHECK) ?
  822. "ACPI_NOTIFY_BUS_CHECK" : "ACPI_NOTIFY_DEVICE_CHECK"));
  823. if (!is_processor_present(handle))
  824. break;
  825. if (acpi_bus_get_device(handle, &device)) {
  826. result = acpi_processor_device_add(handle, &device);
  827. if (result)
  828. printk(KERN_ERR PREFIX
  829. "Unable to add the device\n");
  830. break;
  831. }
  832. pr = acpi_driver_data(device);
  833. if (!pr) {
  834. printk(KERN_ERR PREFIX "Driver data is NULL\n");
  835. break;
  836. }
  837. if (pr->id >= 0 && (pr->id < nr_cpu_ids)) {
  838. kobject_uevent(&device->dev.kobj, KOBJ_OFFLINE);
  839. break;
  840. }
  841. result = acpi_processor_start(device);
  842. if ((!result) && ((pr->id >= 0) && (pr->id < nr_cpu_ids))) {
  843. kobject_uevent(&device->dev.kobj, KOBJ_ONLINE);
  844. } else {
  845. printk(KERN_ERR PREFIX "Device [%s] failed to start\n",
  846. acpi_device_bid(device));
  847. }
  848. break;
  849. case ACPI_NOTIFY_EJECT_REQUEST:
  850. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  851. "received ACPI_NOTIFY_EJECT_REQUEST\n"));
  852. if (acpi_bus_get_device(handle, &device)) {
  853. printk(KERN_ERR PREFIX
  854. "Device don't exist, dropping EJECT\n");
  855. break;
  856. }
  857. pr = acpi_driver_data(device);
  858. if (!pr) {
  859. printk(KERN_ERR PREFIX
  860. "Driver data is NULL, dropping EJECT\n");
  861. return;
  862. }
  863. if ((pr->id < nr_cpu_ids) && (cpu_present(pr->id)))
  864. kobject_uevent(&device->dev.kobj, KOBJ_OFFLINE);
  865. break;
  866. default:
  867. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  868. "Unsupported event [0x%x]\n", event));
  869. break;
  870. }
  871. return;
  872. }
  873. static acpi_status
  874. processor_walk_namespace_cb(acpi_handle handle,
  875. u32 lvl, void *context, void **rv)
  876. {
  877. acpi_status status;
  878. int *action = context;
  879. acpi_object_type type = 0;
  880. status = acpi_get_type(handle, &type);
  881. if (ACPI_FAILURE(status))
  882. return (AE_OK);
  883. if (type != ACPI_TYPE_PROCESSOR)
  884. return (AE_OK);
  885. switch (*action) {
  886. case INSTALL_NOTIFY_HANDLER:
  887. acpi_install_notify_handler(handle,
  888. ACPI_SYSTEM_NOTIFY,
  889. acpi_processor_hotplug_notify,
  890. NULL);
  891. break;
  892. case UNINSTALL_NOTIFY_HANDLER:
  893. acpi_remove_notify_handler(handle,
  894. ACPI_SYSTEM_NOTIFY,
  895. acpi_processor_hotplug_notify);
  896. break;
  897. default:
  898. break;
  899. }
  900. return (AE_OK);
  901. }
  902. static acpi_status acpi_processor_hotadd_init(acpi_handle handle, int *p_cpu)
  903. {
  904. if (!is_processor_present(handle)) {
  905. return AE_ERROR;
  906. }
  907. if (acpi_map_lsapic(handle, p_cpu))
  908. return AE_ERROR;
  909. if (arch_register_cpu(*p_cpu)) {
  910. acpi_unmap_lsapic(*p_cpu);
  911. return AE_ERROR;
  912. }
  913. return AE_OK;
  914. }
  915. static int acpi_processor_handle_eject(struct acpi_processor *pr)
  916. {
  917. if (cpu_online(pr->id))
  918. cpu_down(pr->id);
  919. arch_unregister_cpu(pr->id);
  920. acpi_unmap_lsapic(pr->id);
  921. return (0);
  922. }
  923. #else
  924. static acpi_status acpi_processor_hotadd_init(acpi_handle handle, int *p_cpu)
  925. {
  926. return AE_ERROR;
  927. }
  928. static int acpi_processor_handle_eject(struct acpi_processor *pr)
  929. {
  930. return (-EINVAL);
  931. }
  932. #endif
  933. static
  934. void acpi_processor_install_hotplug_notify(void)
  935. {
  936. #ifdef CONFIG_ACPI_HOTPLUG_CPU
  937. int action = INSTALL_NOTIFY_HANDLER;
  938. acpi_walk_namespace(ACPI_TYPE_PROCESSOR,
  939. ACPI_ROOT_OBJECT,
  940. ACPI_UINT32_MAX,
  941. processor_walk_namespace_cb, &action, NULL);
  942. #endif
  943. register_hotcpu_notifier(&acpi_cpu_notifier);
  944. }
  945. static
  946. void acpi_processor_uninstall_hotplug_notify(void)
  947. {
  948. #ifdef CONFIG_ACPI_HOTPLUG_CPU
  949. int action = UNINSTALL_NOTIFY_HANDLER;
  950. acpi_walk_namespace(ACPI_TYPE_PROCESSOR,
  951. ACPI_ROOT_OBJECT,
  952. ACPI_UINT32_MAX,
  953. processor_walk_namespace_cb, &action, NULL);
  954. #endif
  955. unregister_hotcpu_notifier(&acpi_cpu_notifier);
  956. }
  957. /*
  958. * We keep the driver loaded even when ACPI is not running.
  959. * This is needed for the powernow-k8 driver, that works even without
  960. * ACPI, but needs symbols from this driver
  961. */
  962. static int __init acpi_processor_init(void)
  963. {
  964. int result = 0;
  965. if (acpi_disabled)
  966. return 0;
  967. memset(&errata, 0, sizeof(errata));
  968. #ifdef CONFIG_SMP
  969. if (ACPI_FAILURE(acpi_get_table(ACPI_SIG_MADT, 0,
  970. (struct acpi_table_header **)&madt)))
  971. madt = NULL;
  972. #endif
  973. acpi_processor_dir = proc_mkdir(ACPI_PROCESSOR_CLASS, acpi_root_dir);
  974. if (!acpi_processor_dir)
  975. return -ENOMEM;
  976. /*
  977. * Check whether the system is DMI table. If yes, OSPM
  978. * should not use mwait for CPU-states.
  979. */
  980. dmi_check_system(processor_idle_dmi_table);
  981. result = cpuidle_register_driver(&acpi_idle_driver);
  982. if (result < 0)
  983. goto out_proc;
  984. result = acpi_bus_register_driver(&acpi_processor_driver);
  985. if (result < 0)
  986. goto out_cpuidle;
  987. acpi_processor_install_hotplug_notify();
  988. acpi_thermal_cpufreq_init();
  989. acpi_processor_ppc_init();
  990. acpi_processor_throttling_init();
  991. return 0;
  992. out_cpuidle:
  993. cpuidle_unregister_driver(&acpi_idle_driver);
  994. out_proc:
  995. remove_proc_entry(ACPI_PROCESSOR_CLASS, acpi_root_dir);
  996. return result;
  997. }
  998. static void __exit acpi_processor_exit(void)
  999. {
  1000. if (acpi_disabled)
  1001. return;
  1002. acpi_processor_ppc_exit();
  1003. acpi_thermal_cpufreq_exit();
  1004. acpi_processor_uninstall_hotplug_notify();
  1005. acpi_bus_unregister_driver(&acpi_processor_driver);
  1006. cpuidle_unregister_driver(&acpi_idle_driver);
  1007. remove_proc_entry(ACPI_PROCESSOR_CLASS, acpi_root_dir);
  1008. return;
  1009. }
  1010. module_init(acpi_processor_init);
  1011. module_exit(acpi_processor_exit);
  1012. EXPORT_SYMBOL(acpi_processor_set_thermal_limit);
  1013. MODULE_ALIAS("processor");