processor_core.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221
  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(struct acpi_device *device, u32 event);
  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. {"ACPI0007", 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. .notify = acpi_processor_notify,
  97. },
  98. };
  99. #define INSTALL_NOTIFY_HANDLER 1
  100. #define UNINSTALL_NOTIFY_HANDLER 2
  101. static const struct file_operations acpi_processor_info_fops = {
  102. .owner = THIS_MODULE,
  103. .open = acpi_processor_info_open_fs,
  104. .read = seq_read,
  105. .llseek = seq_lseek,
  106. .release = single_release,
  107. };
  108. DEFINE_PER_CPU(struct acpi_processor *, processors);
  109. struct acpi_processor_errata errata __read_mostly;
  110. static int set_no_mwait(const struct dmi_system_id *id)
  111. {
  112. printk(KERN_NOTICE PREFIX "%s detected - "
  113. "disabling mwait for CPU C-states\n", id->ident);
  114. idle_nomwait = 1;
  115. return 0;
  116. }
  117. static struct dmi_system_id __cpuinitdata processor_idle_dmi_table[] = {
  118. {
  119. set_no_mwait, "IFL91 board", {
  120. DMI_MATCH(DMI_BIOS_VENDOR, "COMPAL"),
  121. DMI_MATCH(DMI_SYS_VENDOR, "ZEPTO"),
  122. DMI_MATCH(DMI_PRODUCT_VERSION, "3215W"),
  123. DMI_MATCH(DMI_BOARD_NAME, "IFL91") }, NULL},
  124. {
  125. set_no_mwait, "Extensa 5220", {
  126. DMI_MATCH(DMI_BIOS_VENDOR, "Phoenix Technologies LTD"),
  127. DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
  128. DMI_MATCH(DMI_PRODUCT_VERSION, "0100"),
  129. DMI_MATCH(DMI_BOARD_NAME, "Columbia") }, NULL},
  130. {},
  131. };
  132. /* --------------------------------------------------------------------------
  133. Errata Handling
  134. -------------------------------------------------------------------------- */
  135. static int acpi_processor_errata_piix4(struct pci_dev *dev)
  136. {
  137. u8 value1 = 0;
  138. u8 value2 = 0;
  139. if (!dev)
  140. return -EINVAL;
  141. /*
  142. * Note that 'dev' references the PIIX4 ACPI Controller.
  143. */
  144. switch (dev->revision) {
  145. case 0:
  146. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found PIIX4 A-step\n"));
  147. break;
  148. case 1:
  149. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found PIIX4 B-step\n"));
  150. break;
  151. case 2:
  152. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found PIIX4E\n"));
  153. break;
  154. case 3:
  155. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found PIIX4M\n"));
  156. break;
  157. default:
  158. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found unknown PIIX4\n"));
  159. break;
  160. }
  161. switch (dev->revision) {
  162. case 0: /* PIIX4 A-step */
  163. case 1: /* PIIX4 B-step */
  164. /*
  165. * See specification changes #13 ("Manual Throttle Duty Cycle")
  166. * and #14 ("Enabling and Disabling Manual Throttle"), plus
  167. * erratum #5 ("STPCLK# Deassertion Time") from the January
  168. * 2002 PIIX4 specification update. Applies to only older
  169. * PIIX4 models.
  170. */
  171. errata.piix4.throttle = 1;
  172. case 2: /* PIIX4E */
  173. case 3: /* PIIX4M */
  174. /*
  175. * See erratum #18 ("C3 Power State/BMIDE and Type-F DMA
  176. * Livelock") from the January 2002 PIIX4 specification update.
  177. * Applies to all PIIX4 models.
  178. */
  179. /*
  180. * BM-IDE
  181. * ------
  182. * Find the PIIX4 IDE Controller and get the Bus Master IDE
  183. * Status register address. We'll use this later to read
  184. * each IDE controller's DMA status to make sure we catch all
  185. * DMA activity.
  186. */
  187. dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
  188. PCI_DEVICE_ID_INTEL_82371AB,
  189. PCI_ANY_ID, PCI_ANY_ID, NULL);
  190. if (dev) {
  191. errata.piix4.bmisx = pci_resource_start(dev, 4);
  192. pci_dev_put(dev);
  193. }
  194. /*
  195. * Type-F DMA
  196. * ----------
  197. * Find the PIIX4 ISA Controller and read the Motherboard
  198. * DMA controller's status to see if Type-F (Fast) DMA mode
  199. * is enabled (bit 7) on either channel. Note that we'll
  200. * disable C3 support if this is enabled, as some legacy
  201. * devices won't operate well if fast DMA is disabled.
  202. */
  203. dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
  204. PCI_DEVICE_ID_INTEL_82371AB_0,
  205. PCI_ANY_ID, PCI_ANY_ID, NULL);
  206. if (dev) {
  207. pci_read_config_byte(dev, 0x76, &value1);
  208. pci_read_config_byte(dev, 0x77, &value2);
  209. if ((value1 & 0x80) || (value2 & 0x80))
  210. errata.piix4.fdma = 1;
  211. pci_dev_put(dev);
  212. }
  213. break;
  214. }
  215. if (errata.piix4.bmisx)
  216. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  217. "Bus master activity detection (BM-IDE) erratum enabled\n"));
  218. if (errata.piix4.fdma)
  219. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  220. "Type-F DMA livelock erratum (C3 disabled)\n"));
  221. return 0;
  222. }
  223. static int acpi_processor_errata(struct acpi_processor *pr)
  224. {
  225. int result = 0;
  226. struct pci_dev *dev = NULL;
  227. if (!pr)
  228. return -EINVAL;
  229. /*
  230. * PIIX4
  231. */
  232. dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
  233. PCI_DEVICE_ID_INTEL_82371AB_3, PCI_ANY_ID,
  234. PCI_ANY_ID, NULL);
  235. if (dev) {
  236. result = acpi_processor_errata_piix4(dev);
  237. pci_dev_put(dev);
  238. }
  239. return result;
  240. }
  241. /* --------------------------------------------------------------------------
  242. Common ACPI processor functions
  243. -------------------------------------------------------------------------- */
  244. /*
  245. * _PDC is required for a BIOS-OS handshake for most of the newer
  246. * ACPI processor features.
  247. */
  248. static int acpi_processor_set_pdc(struct acpi_processor *pr)
  249. {
  250. struct acpi_object_list *pdc_in = pr->pdc;
  251. acpi_status status = AE_OK;
  252. if (!pdc_in)
  253. return status;
  254. if (idle_nomwait) {
  255. /*
  256. * If mwait is disabled for CPU C-states, the C2C3_FFH access
  257. * mode will be disabled in the parameter of _PDC object.
  258. * Of course C1_FFH access mode will also be disabled.
  259. */
  260. union acpi_object *obj;
  261. u32 *buffer = NULL;
  262. obj = pdc_in->pointer;
  263. buffer = (u32 *)(obj->buffer.pointer);
  264. buffer[2] &= ~(ACPI_PDC_C_C2C3_FFH | ACPI_PDC_C_C1_FFH);
  265. }
  266. status = acpi_evaluate_object(pr->handle, "_PDC", pdc_in, NULL);
  267. if (ACPI_FAILURE(status))
  268. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  269. "Could not evaluate _PDC, using legacy perf. control...\n"));
  270. return status;
  271. }
  272. /* --------------------------------------------------------------------------
  273. FS Interface (/proc)
  274. -------------------------------------------------------------------------- */
  275. static struct proc_dir_entry *acpi_processor_dir = NULL;
  276. static int acpi_processor_info_seq_show(struct seq_file *seq, void *offset)
  277. {
  278. struct acpi_processor *pr = seq->private;
  279. if (!pr)
  280. goto end;
  281. seq_printf(seq, "processor id: %d\n"
  282. "acpi id: %d\n"
  283. "bus mastering control: %s\n"
  284. "power management: %s\n"
  285. "throttling control: %s\n"
  286. "limit interface: %s\n",
  287. pr->id,
  288. pr->acpi_id,
  289. pr->flags.bm_control ? "yes" : "no",
  290. pr->flags.power ? "yes" : "no",
  291. pr->flags.throttling ? "yes" : "no",
  292. pr->flags.limit ? "yes" : "no");
  293. end:
  294. return 0;
  295. }
  296. static int acpi_processor_info_open_fs(struct inode *inode, struct file *file)
  297. {
  298. return single_open(file, acpi_processor_info_seq_show,
  299. PDE(inode)->data);
  300. }
  301. static int acpi_processor_add_fs(struct acpi_device *device)
  302. {
  303. struct proc_dir_entry *entry = NULL;
  304. if (!acpi_device_dir(device)) {
  305. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  306. acpi_processor_dir);
  307. if (!acpi_device_dir(device))
  308. return -ENODEV;
  309. }
  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_OBJECT_HID)) {
  508. /* Declared with "Processor" statement; match ProcessorID */
  509. status = acpi_evaluate_object(pr->handle, NULL, NULL, &buffer);
  510. if (ACPI_FAILURE(status)) {
  511. printk(KERN_ERR PREFIX "Evaluating processor object\n");
  512. return -ENODEV;
  513. }
  514. /*
  515. * TBD: Synch processor ID (via LAPIC/LSAPIC structures) on SMP.
  516. * >>> 'acpi_get_processor_id(acpi_id, &id)' in
  517. * arch/xxx/acpi.c
  518. */
  519. pr->acpi_id = object.processor.proc_id;
  520. } else {
  521. /*
  522. * Declared with "Device" statement; match _UID.
  523. * Note that we don't handle string _UIDs yet.
  524. */
  525. unsigned long long value;
  526. status = acpi_evaluate_integer(pr->handle, METHOD_NAME__UID,
  527. NULL, &value);
  528. if (ACPI_FAILURE(status)) {
  529. printk(KERN_ERR PREFIX
  530. "Evaluating processor _UID [%#x]\n", status);
  531. return -ENODEV;
  532. }
  533. device_declaration = 1;
  534. pr->acpi_id = value;
  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. /*
  556. * On some boxes several processors use the same processor bus id.
  557. * But they are located in different scope. For example:
  558. * \_SB.SCK0.CPU0
  559. * \_SB.SCK1.CPU0
  560. * Rename the processor device bus id. And the new bus id will be
  561. * generated as the following format:
  562. * CPU+CPU ID.
  563. */
  564. sprintf(acpi_device_bid(device), "CPU%X", pr->id);
  565. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Processor [%d:%d]\n", pr->id,
  566. pr->acpi_id));
  567. if (!object.processor.pblk_address)
  568. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No PBLK (NULL address)\n"));
  569. else if (object.processor.pblk_length != 6)
  570. printk(KERN_ERR PREFIX "Invalid PBLK length [%d]\n",
  571. object.processor.pblk_length);
  572. else {
  573. pr->throttling.address = object.processor.pblk_address;
  574. pr->throttling.duty_offset = acpi_gbl_FADT.duty_offset;
  575. pr->throttling.duty_width = acpi_gbl_FADT.duty_width;
  576. pr->pblk = object.processor.pblk_address;
  577. /*
  578. * We don't care about error returns - we just try to mark
  579. * these reserved so that nobody else is confused into thinking
  580. * that this region might be unused..
  581. *
  582. * (In particular, allocating the IO range for Cardbus)
  583. */
  584. request_region(pr->throttling.address, 6, "ACPI CPU throttle");
  585. }
  586. /*
  587. * If ACPI describes a slot number for this CPU, we can use it
  588. * ensure we get the right value in the "physical id" field
  589. * of /proc/cpuinfo
  590. */
  591. status = acpi_evaluate_object(pr->handle, "_SUN", NULL, &buffer);
  592. if (ACPI_SUCCESS(status))
  593. arch_fix_phys_package_id(pr->id, object.integer.value);
  594. return 0;
  595. }
  596. static DEFINE_PER_CPU(void *, processor_device_array);
  597. static void acpi_processor_notify(struct acpi_device *device, u32 event)
  598. {
  599. struct acpi_processor *pr = acpi_driver_data(device);
  600. int saved;
  601. if (!pr)
  602. return;
  603. switch (event) {
  604. case ACPI_PROCESSOR_NOTIFY_PERFORMANCE:
  605. saved = pr->performance_platform_limit;
  606. acpi_processor_ppc_has_changed(pr);
  607. if (saved == pr->performance_platform_limit)
  608. break;
  609. acpi_bus_generate_proc_event(device, event,
  610. pr->performance_platform_limit);
  611. acpi_bus_generate_netlink_event(device->pnp.device_class,
  612. dev_name(&device->dev), event,
  613. pr->performance_platform_limit);
  614. break;
  615. case ACPI_PROCESSOR_NOTIFY_POWER:
  616. acpi_processor_cst_has_changed(pr);
  617. acpi_bus_generate_proc_event(device, event, 0);
  618. acpi_bus_generate_netlink_event(device->pnp.device_class,
  619. dev_name(&device->dev), event, 0);
  620. break;
  621. case ACPI_PROCESSOR_NOTIFY_THROTTLING:
  622. acpi_processor_tstate_has_changed(pr);
  623. acpi_bus_generate_proc_event(device, event, 0);
  624. acpi_bus_generate_netlink_event(device->pnp.device_class,
  625. dev_name(&device->dev), event, 0);
  626. default:
  627. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  628. "Unsupported event [0x%x]\n", event));
  629. break;
  630. }
  631. return;
  632. }
  633. static int acpi_cpu_soft_notify(struct notifier_block *nfb,
  634. unsigned long action, void *hcpu)
  635. {
  636. unsigned int cpu = (unsigned long)hcpu;
  637. struct acpi_processor *pr = per_cpu(processors, cpu);
  638. if (action == CPU_ONLINE && pr) {
  639. acpi_processor_ppc_has_changed(pr);
  640. acpi_processor_cst_has_changed(pr);
  641. acpi_processor_tstate_has_changed(pr);
  642. }
  643. return NOTIFY_OK;
  644. }
  645. static struct notifier_block acpi_cpu_notifier =
  646. {
  647. .notifier_call = acpi_cpu_soft_notify,
  648. };
  649. static int acpi_processor_add(struct acpi_device *device)
  650. {
  651. struct acpi_processor *pr = NULL;
  652. if (!device)
  653. return -EINVAL;
  654. pr = kzalloc(sizeof(struct acpi_processor), GFP_KERNEL);
  655. if (!pr)
  656. return -ENOMEM;
  657. if (!zalloc_cpumask_var(&pr->throttling.shared_cpu_map, GFP_KERNEL)) {
  658. kfree(pr);
  659. return -ENOMEM;
  660. }
  661. pr->handle = device->handle;
  662. strcpy(acpi_device_name(device), ACPI_PROCESSOR_DEVICE_NAME);
  663. strcpy(acpi_device_class(device), ACPI_PROCESSOR_CLASS);
  664. device->driver_data = pr;
  665. return 0;
  666. }
  667. static int __cpuinit acpi_processor_start(struct acpi_device *device)
  668. {
  669. int result = 0;
  670. struct acpi_processor *pr;
  671. struct sys_device *sysdev;
  672. pr = acpi_driver_data(device);
  673. result = acpi_processor_get_info(device);
  674. if (result) {
  675. /* Processor is physically not present */
  676. return 0;
  677. }
  678. BUG_ON((pr->id >= nr_cpu_ids) || (pr->id < 0));
  679. /*
  680. * Buggy BIOS check
  681. * ACPI id of processors can be reported wrongly by the BIOS.
  682. * Don't trust it blindly
  683. */
  684. if (per_cpu(processor_device_array, pr->id) != NULL &&
  685. per_cpu(processor_device_array, pr->id) != device) {
  686. printk(KERN_WARNING "BIOS reported wrong ACPI id "
  687. "for the processor\n");
  688. return -ENODEV;
  689. }
  690. per_cpu(processor_device_array, pr->id) = device;
  691. per_cpu(processors, pr->id) = pr;
  692. result = acpi_processor_add_fs(device);
  693. if (result)
  694. return result;
  695. sysdev = get_cpu_sysdev(pr->id);
  696. if (sysfs_create_link(&device->dev.kobj, &sysdev->kobj, "sysdev")) {
  697. result = -EFAULT;
  698. goto err_remove_fs;
  699. }
  700. /* _PDC call should be done before doing anything else (if reqd.). */
  701. arch_acpi_processor_init_pdc(pr);
  702. acpi_processor_set_pdc(pr);
  703. arch_acpi_processor_cleanup_pdc(pr);
  704. #ifdef CONFIG_CPU_FREQ
  705. acpi_processor_ppc_has_changed(pr);
  706. #endif
  707. acpi_processor_get_throttling_info(pr);
  708. acpi_processor_get_limit_info(pr);
  709. acpi_processor_power_init(pr, device);
  710. pr->cdev = thermal_cooling_device_register("Processor", device,
  711. &processor_cooling_ops);
  712. if (IS_ERR(pr->cdev)) {
  713. result = PTR_ERR(pr->cdev);
  714. goto err_power_exit;
  715. }
  716. dev_info(&device->dev, "registered as cooling_device%d\n",
  717. pr->cdev->id);
  718. result = sysfs_create_link(&device->dev.kobj,
  719. &pr->cdev->device.kobj,
  720. "thermal_cooling");
  721. if (result) {
  722. printk(KERN_ERR PREFIX "Create sysfs link\n");
  723. goto err_thermal_unregister;
  724. }
  725. result = sysfs_create_link(&pr->cdev->device.kobj,
  726. &device->dev.kobj,
  727. "device");
  728. if (result) {
  729. printk(KERN_ERR PREFIX "Create sysfs link\n");
  730. goto err_remove_sysfs;
  731. }
  732. if (pr->flags.throttling) {
  733. printk(KERN_INFO PREFIX "%s [%s] (supports",
  734. acpi_device_name(device), acpi_device_bid(device));
  735. printk(" %d throttling states", pr->throttling.state_count);
  736. printk(")\n");
  737. }
  738. return 0;
  739. err_remove_sysfs:
  740. sysfs_remove_link(&device->dev.kobj, "thermal_cooling");
  741. err_thermal_unregister:
  742. thermal_cooling_device_unregister(pr->cdev);
  743. err_power_exit:
  744. acpi_processor_power_exit(pr, device);
  745. err_remove_fs:
  746. acpi_processor_remove_fs(device);
  747. return result;
  748. }
  749. static int acpi_processor_remove(struct acpi_device *device, int type)
  750. {
  751. struct acpi_processor *pr = NULL;
  752. if (!device || !acpi_driver_data(device))
  753. return -EINVAL;
  754. pr = acpi_driver_data(device);
  755. if (pr->id >= nr_cpu_ids)
  756. goto free;
  757. if (type == ACPI_BUS_REMOVAL_EJECT) {
  758. if (acpi_processor_handle_eject(pr))
  759. return -EINVAL;
  760. }
  761. acpi_processor_power_exit(pr, device);
  762. sysfs_remove_link(&device->dev.kobj, "sysdev");
  763. acpi_processor_remove_fs(device);
  764. if (pr->cdev) {
  765. sysfs_remove_link(&device->dev.kobj, "thermal_cooling");
  766. sysfs_remove_link(&pr->cdev->device.kobj, "device");
  767. thermal_cooling_device_unregister(pr->cdev);
  768. pr->cdev = NULL;
  769. }
  770. per_cpu(processors, pr->id) = NULL;
  771. per_cpu(processor_device_array, pr->id) = NULL;
  772. free:
  773. free_cpumask_var(pr->throttling.shared_cpu_map);
  774. kfree(pr);
  775. return 0;
  776. }
  777. #ifdef CONFIG_ACPI_HOTPLUG_CPU
  778. /****************************************************************************
  779. * Acpi processor hotplug support *
  780. ****************************************************************************/
  781. static int is_processor_present(acpi_handle handle)
  782. {
  783. acpi_status status;
  784. unsigned long long sta = 0;
  785. status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
  786. if (ACPI_SUCCESS(status) && (sta & ACPI_STA_DEVICE_PRESENT))
  787. return 1;
  788. /*
  789. * _STA is mandatory for a processor that supports hot plug
  790. */
  791. if (status == AE_NOT_FOUND)
  792. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  793. "Processor does not support hot plug\n"));
  794. else
  795. ACPI_EXCEPTION((AE_INFO, status,
  796. "Processor Device is not present"));
  797. return 0;
  798. }
  799. static
  800. int acpi_processor_device_add(acpi_handle handle, struct acpi_device **device)
  801. {
  802. acpi_handle phandle;
  803. struct acpi_device *pdev;
  804. struct acpi_processor *pr;
  805. if (acpi_get_parent(handle, &phandle)) {
  806. return -ENODEV;
  807. }
  808. if (acpi_bus_get_device(phandle, &pdev)) {
  809. return -ENODEV;
  810. }
  811. if (acpi_bus_add(device, pdev, handle, ACPI_BUS_TYPE_PROCESSOR)) {
  812. return -ENODEV;
  813. }
  814. acpi_bus_start(*device);
  815. pr = acpi_driver_data(*device);
  816. if (!pr)
  817. return -ENODEV;
  818. return 0;
  819. }
  820. static void __ref acpi_processor_hotplug_notify(acpi_handle handle,
  821. u32 event, void *data)
  822. {
  823. struct acpi_processor *pr;
  824. struct acpi_device *device = NULL;
  825. int result;
  826. switch (event) {
  827. case ACPI_NOTIFY_BUS_CHECK:
  828. case ACPI_NOTIFY_DEVICE_CHECK:
  829. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  830. "Processor driver received %s event\n",
  831. (event == ACPI_NOTIFY_BUS_CHECK) ?
  832. "ACPI_NOTIFY_BUS_CHECK" : "ACPI_NOTIFY_DEVICE_CHECK"));
  833. if (!is_processor_present(handle))
  834. break;
  835. if (acpi_bus_get_device(handle, &device)) {
  836. result = acpi_processor_device_add(handle, &device);
  837. if (result)
  838. printk(KERN_ERR PREFIX
  839. "Unable to add the device\n");
  840. break;
  841. }
  842. pr = acpi_driver_data(device);
  843. if (!pr) {
  844. printk(KERN_ERR PREFIX "Driver data is NULL\n");
  845. break;
  846. }
  847. result = acpi_processor_start(device);
  848. if (result)
  849. printk(KERN_ERR PREFIX "Device [%s] failed to start\n",
  850. acpi_device_bid(device));
  851. break;
  852. case ACPI_NOTIFY_EJECT_REQUEST:
  853. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  854. "received ACPI_NOTIFY_EJECT_REQUEST\n"));
  855. if (acpi_bus_get_device(handle, &device)) {
  856. printk(KERN_ERR PREFIX
  857. "Device don't exist, dropping EJECT\n");
  858. break;
  859. }
  860. pr = acpi_driver_data(device);
  861. if (!pr) {
  862. printk(KERN_ERR PREFIX
  863. "Driver data is NULL, dropping EJECT\n");
  864. return;
  865. }
  866. break;
  867. default:
  868. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  869. "Unsupported event [0x%x]\n", event));
  870. break;
  871. }
  872. return;
  873. }
  874. static acpi_status
  875. processor_walk_namespace_cb(acpi_handle handle,
  876. u32 lvl, void *context, void **rv)
  877. {
  878. acpi_status status;
  879. int *action = context;
  880. acpi_object_type type = 0;
  881. status = acpi_get_type(handle, &type);
  882. if (ACPI_FAILURE(status))
  883. return (AE_OK);
  884. if (type != ACPI_TYPE_PROCESSOR)
  885. return (AE_OK);
  886. switch (*action) {
  887. case INSTALL_NOTIFY_HANDLER:
  888. acpi_install_notify_handler(handle,
  889. ACPI_SYSTEM_NOTIFY,
  890. acpi_processor_hotplug_notify,
  891. NULL);
  892. break;
  893. case UNINSTALL_NOTIFY_HANDLER:
  894. acpi_remove_notify_handler(handle,
  895. ACPI_SYSTEM_NOTIFY,
  896. acpi_processor_hotplug_notify);
  897. break;
  898. default:
  899. break;
  900. }
  901. return (AE_OK);
  902. }
  903. static acpi_status acpi_processor_hotadd_init(acpi_handle handle, int *p_cpu)
  904. {
  905. if (!is_processor_present(handle)) {
  906. return AE_ERROR;
  907. }
  908. if (acpi_map_lsapic(handle, p_cpu))
  909. return AE_ERROR;
  910. if (arch_register_cpu(*p_cpu)) {
  911. acpi_unmap_lsapic(*p_cpu);
  912. return AE_ERROR;
  913. }
  914. return AE_OK;
  915. }
  916. static int acpi_processor_handle_eject(struct acpi_processor *pr)
  917. {
  918. if (cpu_online(pr->id))
  919. cpu_down(pr->id);
  920. arch_unregister_cpu(pr->id);
  921. acpi_unmap_lsapic(pr->id);
  922. return (0);
  923. }
  924. #else
  925. static acpi_status acpi_processor_hotadd_init(acpi_handle handle, int *p_cpu)
  926. {
  927. return AE_ERROR;
  928. }
  929. static int acpi_processor_handle_eject(struct acpi_processor *pr)
  930. {
  931. return (-EINVAL);
  932. }
  933. #endif
  934. static
  935. void acpi_processor_install_hotplug_notify(void)
  936. {
  937. #ifdef CONFIG_ACPI_HOTPLUG_CPU
  938. int action = INSTALL_NOTIFY_HANDLER;
  939. acpi_walk_namespace(ACPI_TYPE_PROCESSOR,
  940. ACPI_ROOT_OBJECT,
  941. ACPI_UINT32_MAX,
  942. processor_walk_namespace_cb, &action, NULL);
  943. #endif
  944. register_hotcpu_notifier(&acpi_cpu_notifier);
  945. }
  946. static
  947. void acpi_processor_uninstall_hotplug_notify(void)
  948. {
  949. #ifdef CONFIG_ACPI_HOTPLUG_CPU
  950. int action = UNINSTALL_NOTIFY_HANDLER;
  951. acpi_walk_namespace(ACPI_TYPE_PROCESSOR,
  952. ACPI_ROOT_OBJECT,
  953. ACPI_UINT32_MAX,
  954. processor_walk_namespace_cb, &action, NULL);
  955. #endif
  956. unregister_hotcpu_notifier(&acpi_cpu_notifier);
  957. }
  958. /*
  959. * We keep the driver loaded even when ACPI is not running.
  960. * This is needed for the powernow-k8 driver, that works even without
  961. * ACPI, but needs symbols from this driver
  962. */
  963. static int __init acpi_processor_init(void)
  964. {
  965. int result = 0;
  966. memset(&errata, 0, sizeof(errata));
  967. #ifdef CONFIG_SMP
  968. if (ACPI_FAILURE(acpi_get_table(ACPI_SIG_MADT, 0,
  969. (struct acpi_table_header **)&madt)))
  970. madt = NULL;
  971. #endif
  972. acpi_processor_dir = proc_mkdir(ACPI_PROCESSOR_CLASS, acpi_root_dir);
  973. if (!acpi_processor_dir)
  974. return -ENOMEM;
  975. /*
  976. * Check whether the system is DMI table. If yes, OSPM
  977. * should not use mwait for CPU-states.
  978. */
  979. dmi_check_system(processor_idle_dmi_table);
  980. result = cpuidle_register_driver(&acpi_idle_driver);
  981. if (result < 0)
  982. goto out_proc;
  983. result = acpi_bus_register_driver(&acpi_processor_driver);
  984. if (result < 0)
  985. goto out_cpuidle;
  986. acpi_processor_install_hotplug_notify();
  987. acpi_thermal_cpufreq_init();
  988. acpi_processor_ppc_init();
  989. acpi_processor_throttling_init();
  990. return 0;
  991. out_cpuidle:
  992. cpuidle_unregister_driver(&acpi_idle_driver);
  993. out_proc:
  994. remove_proc_entry(ACPI_PROCESSOR_CLASS, acpi_root_dir);
  995. return result;
  996. }
  997. static void __exit acpi_processor_exit(void)
  998. {
  999. acpi_processor_ppc_exit();
  1000. acpi_thermal_cpufreq_exit();
  1001. acpi_processor_uninstall_hotplug_notify();
  1002. acpi_bus_unregister_driver(&acpi_processor_driver);
  1003. cpuidle_unregister_driver(&acpi_idle_driver);
  1004. remove_proc_entry(ACPI_PROCESSOR_CLASS, acpi_root_dir);
  1005. return;
  1006. }
  1007. module_init(acpi_processor_init);
  1008. module_exit(acpi_processor_exit);
  1009. EXPORT_SYMBOL(acpi_processor_set_thermal_limit);
  1010. MODULE_ALIAS("processor");