processor_core.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209
  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 __cpuinit 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. return 0;
  740. err_remove_sysfs:
  741. sysfs_remove_link(&device->dev.kobj, "thermal_cooling");
  742. err_thermal_unregister:
  743. thermal_cooling_device_unregister(pr->cdev);
  744. err_power_exit:
  745. acpi_processor_power_exit(pr, device);
  746. err_remove_fs:
  747. acpi_processor_remove_fs(device);
  748. err_free_cpumask:
  749. free_cpumask_var(pr->throttling.shared_cpu_map);
  750. return result;
  751. }
  752. static int acpi_processor_remove(struct acpi_device *device, int type)
  753. {
  754. struct acpi_processor *pr = NULL;
  755. if (!device || !acpi_driver_data(device))
  756. return -EINVAL;
  757. pr = acpi_driver_data(device);
  758. if (pr->id >= nr_cpu_ids)
  759. goto free;
  760. if (type == ACPI_BUS_REMOVAL_EJECT) {
  761. if (acpi_processor_handle_eject(pr))
  762. return -EINVAL;
  763. }
  764. acpi_processor_power_exit(pr, device);
  765. sysfs_remove_link(&device->dev.kobj, "sysdev");
  766. acpi_processor_remove_fs(device);
  767. if (pr->cdev) {
  768. sysfs_remove_link(&device->dev.kobj, "thermal_cooling");
  769. sysfs_remove_link(&pr->cdev->device.kobj, "device");
  770. thermal_cooling_device_unregister(pr->cdev);
  771. pr->cdev = NULL;
  772. }
  773. per_cpu(processors, pr->id) = NULL;
  774. per_cpu(processor_device_array, pr->id) = NULL;
  775. free:
  776. free_cpumask_var(pr->throttling.shared_cpu_map);
  777. kfree(pr);
  778. return 0;
  779. }
  780. #ifdef CONFIG_ACPI_HOTPLUG_CPU
  781. /****************************************************************************
  782. * Acpi processor hotplug support *
  783. ****************************************************************************/
  784. static int is_processor_present(acpi_handle handle)
  785. {
  786. acpi_status status;
  787. unsigned long long sta = 0;
  788. status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
  789. if (ACPI_SUCCESS(status) && (sta & ACPI_STA_DEVICE_PRESENT))
  790. return 1;
  791. /*
  792. * _STA is mandatory for a processor that supports hot plug
  793. */
  794. if (status == AE_NOT_FOUND)
  795. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  796. "Processor does not support hot plug\n"));
  797. else
  798. ACPI_EXCEPTION((AE_INFO, status,
  799. "Processor Device is not present"));
  800. return 0;
  801. }
  802. static
  803. int acpi_processor_device_add(acpi_handle handle, struct acpi_device **device)
  804. {
  805. acpi_handle phandle;
  806. struct acpi_device *pdev;
  807. if (acpi_get_parent(handle, &phandle)) {
  808. return -ENODEV;
  809. }
  810. if (acpi_bus_get_device(phandle, &pdev)) {
  811. return -ENODEV;
  812. }
  813. if (acpi_bus_add(device, pdev, handle, ACPI_BUS_TYPE_PROCESSOR)) {
  814. return -ENODEV;
  815. }
  816. return 0;
  817. }
  818. static void __ref acpi_processor_hotplug_notify(acpi_handle handle,
  819. u32 event, void *data)
  820. {
  821. struct acpi_processor *pr;
  822. struct acpi_device *device = NULL;
  823. int result;
  824. switch (event) {
  825. case ACPI_NOTIFY_BUS_CHECK:
  826. case ACPI_NOTIFY_DEVICE_CHECK:
  827. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  828. "Processor driver received %s event\n",
  829. (event == ACPI_NOTIFY_BUS_CHECK) ?
  830. "ACPI_NOTIFY_BUS_CHECK" : "ACPI_NOTIFY_DEVICE_CHECK"));
  831. if (!is_processor_present(handle))
  832. break;
  833. if (acpi_bus_get_device(handle, &device)) {
  834. result = acpi_processor_device_add(handle, &device);
  835. if (result)
  836. printk(KERN_ERR PREFIX
  837. "Unable to add the device\n");
  838. break;
  839. }
  840. break;
  841. case ACPI_NOTIFY_EJECT_REQUEST:
  842. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  843. "received ACPI_NOTIFY_EJECT_REQUEST\n"));
  844. if (acpi_bus_get_device(handle, &device)) {
  845. printk(KERN_ERR PREFIX
  846. "Device don't exist, dropping EJECT\n");
  847. break;
  848. }
  849. pr = acpi_driver_data(device);
  850. if (!pr) {
  851. printk(KERN_ERR PREFIX
  852. "Driver data is NULL, dropping EJECT\n");
  853. return;
  854. }
  855. break;
  856. default:
  857. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  858. "Unsupported event [0x%x]\n", event));
  859. break;
  860. }
  861. return;
  862. }
  863. static acpi_status
  864. processor_walk_namespace_cb(acpi_handle handle,
  865. u32 lvl, void *context, void **rv)
  866. {
  867. acpi_status status;
  868. int *action = context;
  869. acpi_object_type type = 0;
  870. status = acpi_get_type(handle, &type);
  871. if (ACPI_FAILURE(status))
  872. return (AE_OK);
  873. if (type != ACPI_TYPE_PROCESSOR)
  874. return (AE_OK);
  875. switch (*action) {
  876. case INSTALL_NOTIFY_HANDLER:
  877. acpi_install_notify_handler(handle,
  878. ACPI_SYSTEM_NOTIFY,
  879. acpi_processor_hotplug_notify,
  880. NULL);
  881. break;
  882. case UNINSTALL_NOTIFY_HANDLER:
  883. acpi_remove_notify_handler(handle,
  884. ACPI_SYSTEM_NOTIFY,
  885. acpi_processor_hotplug_notify);
  886. break;
  887. default:
  888. break;
  889. }
  890. return (AE_OK);
  891. }
  892. static acpi_status acpi_processor_hotadd_init(acpi_handle handle, int *p_cpu)
  893. {
  894. if (!is_processor_present(handle)) {
  895. return AE_ERROR;
  896. }
  897. if (acpi_map_lsapic(handle, p_cpu))
  898. return AE_ERROR;
  899. if (arch_register_cpu(*p_cpu)) {
  900. acpi_unmap_lsapic(*p_cpu);
  901. return AE_ERROR;
  902. }
  903. return AE_OK;
  904. }
  905. static int acpi_processor_handle_eject(struct acpi_processor *pr)
  906. {
  907. if (cpu_online(pr->id))
  908. cpu_down(pr->id);
  909. arch_unregister_cpu(pr->id);
  910. acpi_unmap_lsapic(pr->id);
  911. return (0);
  912. }
  913. #else
  914. static acpi_status acpi_processor_hotadd_init(acpi_handle handle, int *p_cpu)
  915. {
  916. return AE_ERROR;
  917. }
  918. static int acpi_processor_handle_eject(struct acpi_processor *pr)
  919. {
  920. return (-EINVAL);
  921. }
  922. #endif
  923. static
  924. void acpi_processor_install_hotplug_notify(void)
  925. {
  926. #ifdef CONFIG_ACPI_HOTPLUG_CPU
  927. int action = INSTALL_NOTIFY_HANDLER;
  928. acpi_walk_namespace(ACPI_TYPE_PROCESSOR,
  929. ACPI_ROOT_OBJECT,
  930. ACPI_UINT32_MAX,
  931. processor_walk_namespace_cb, NULL, &action, NULL);
  932. #endif
  933. register_hotcpu_notifier(&acpi_cpu_notifier);
  934. }
  935. static
  936. void acpi_processor_uninstall_hotplug_notify(void)
  937. {
  938. #ifdef CONFIG_ACPI_HOTPLUG_CPU
  939. int action = UNINSTALL_NOTIFY_HANDLER;
  940. acpi_walk_namespace(ACPI_TYPE_PROCESSOR,
  941. ACPI_ROOT_OBJECT,
  942. ACPI_UINT32_MAX,
  943. processor_walk_namespace_cb, NULL, &action, NULL);
  944. #endif
  945. unregister_hotcpu_notifier(&acpi_cpu_notifier);
  946. }
  947. /*
  948. * We keep the driver loaded even when ACPI is not running.
  949. * This is needed for the powernow-k8 driver, that works even without
  950. * ACPI, but needs symbols from this driver
  951. */
  952. static int __init acpi_processor_init(void)
  953. {
  954. int result = 0;
  955. if (acpi_disabled)
  956. return 0;
  957. memset(&errata, 0, sizeof(errata));
  958. #ifdef CONFIG_SMP
  959. if (ACPI_FAILURE(acpi_get_table(ACPI_SIG_MADT, 0,
  960. (struct acpi_table_header **)&madt)))
  961. madt = NULL;
  962. #endif
  963. #ifdef CONFIG_ACPI_PROCFS
  964. acpi_processor_dir = proc_mkdir(ACPI_PROCESSOR_CLASS, acpi_root_dir);
  965. if (!acpi_processor_dir)
  966. return -ENOMEM;
  967. #endif
  968. /*
  969. * Check whether the system is DMI table. If yes, OSPM
  970. * should not use mwait for CPU-states.
  971. */
  972. dmi_check_system(processor_idle_dmi_table);
  973. result = cpuidle_register_driver(&acpi_idle_driver);
  974. if (result < 0)
  975. goto out_proc;
  976. result = acpi_bus_register_driver(&acpi_processor_driver);
  977. if (result < 0)
  978. goto out_cpuidle;
  979. acpi_processor_install_hotplug_notify();
  980. acpi_thermal_cpufreq_init();
  981. acpi_processor_ppc_init();
  982. acpi_processor_throttling_init();
  983. return 0;
  984. out_cpuidle:
  985. cpuidle_unregister_driver(&acpi_idle_driver);
  986. out_proc:
  987. #ifdef CONFIG_ACPI_PROCFS
  988. remove_proc_entry(ACPI_PROCESSOR_CLASS, acpi_root_dir);
  989. #endif
  990. return result;
  991. }
  992. static void __exit acpi_processor_exit(void)
  993. {
  994. if (acpi_disabled)
  995. return;
  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. #ifdef CONFIG_ACPI_PROCFS
  1002. remove_proc_entry(ACPI_PROCESSOR_CLASS, acpi_root_dir);
  1003. #endif
  1004. return;
  1005. }
  1006. module_init(acpi_processor_init);
  1007. module_exit(acpi_processor_exit);
  1008. EXPORT_SYMBOL(acpi_processor_set_thermal_limit);
  1009. MODULE_ALIAS("processor");