processor_core.c 31 KB

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