processor_core.c 31 KB

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