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 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. {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. .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_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. struct acpi_processor *pr;
  591. struct sys_device *sysdev;
  592. pr = acpi_driver_data(device);
  593. result = acpi_processor_get_info(device);
  594. if (result) {
  595. /* Processor is physically not present */
  596. return 0;
  597. }
  598. BUG_ON((pr->id >= nr_cpu_ids) || (pr->id < 0));
  599. /*
  600. * Buggy BIOS check
  601. * ACPI id of processors can be reported wrongly by the BIOS.
  602. * Don't trust it blindly
  603. */
  604. if (per_cpu(processor_device_array, pr->id) != NULL &&
  605. per_cpu(processor_device_array, pr->id) != device) {
  606. printk(KERN_WARNING "BIOS reported wrong ACPI id "
  607. "for the processor\n");
  608. return -ENODEV;
  609. }
  610. per_cpu(processor_device_array, pr->id) = device;
  611. per_cpu(processors, pr->id) = pr;
  612. result = acpi_processor_add_fs(device);
  613. if (result)
  614. goto end;
  615. sysdev = get_cpu_sysdev(pr->id);
  616. if (sysfs_create_link(&device->dev.kobj, &sysdev->kobj, "sysdev"))
  617. return -EFAULT;
  618. /* _PDC call should be done before doing anything else (if reqd.). */
  619. arch_acpi_processor_init_pdc(pr);
  620. acpi_processor_set_pdc(pr);
  621. #ifdef CONFIG_CPU_FREQ
  622. acpi_processor_ppc_has_changed(pr);
  623. #endif
  624. acpi_processor_get_throttling_info(pr);
  625. acpi_processor_get_limit_info(pr);
  626. acpi_processor_power_init(pr, device);
  627. pr->cdev = thermal_cooling_device_register("Processor", device,
  628. &processor_cooling_ops);
  629. if (IS_ERR(pr->cdev)) {
  630. result = PTR_ERR(pr->cdev);
  631. goto end;
  632. }
  633. dev_info(&device->dev, "registered as cooling_device%d\n",
  634. pr->cdev->id);
  635. result = sysfs_create_link(&device->dev.kobj,
  636. &pr->cdev->device.kobj,
  637. "thermal_cooling");
  638. if (result)
  639. printk(KERN_ERR PREFIX "Create sysfs link\n");
  640. result = sysfs_create_link(&pr->cdev->device.kobj,
  641. &device->dev.kobj,
  642. "device");
  643. if (result)
  644. printk(KERN_ERR PREFIX "Create sysfs link\n");
  645. if (pr->flags.throttling) {
  646. printk(KERN_INFO PREFIX "%s [%s] (supports",
  647. acpi_device_name(device), acpi_device_bid(device));
  648. printk(" %d throttling states", pr->throttling.state_count);
  649. printk(")\n");
  650. }
  651. end:
  652. return result;
  653. }
  654. static void acpi_processor_notify(struct acpi_device *device, u32 event)
  655. {
  656. struct acpi_processor *pr = acpi_driver_data(device);
  657. int saved;
  658. if (!pr)
  659. return;
  660. switch (event) {
  661. case ACPI_PROCESSOR_NOTIFY_PERFORMANCE:
  662. saved = pr->performance_platform_limit;
  663. acpi_processor_ppc_has_changed(pr);
  664. if (saved == pr->performance_platform_limit)
  665. break;
  666. acpi_bus_generate_proc_event(device, event,
  667. pr->performance_platform_limit);
  668. acpi_bus_generate_netlink_event(device->pnp.device_class,
  669. dev_name(&device->dev), event,
  670. pr->performance_platform_limit);
  671. break;
  672. case ACPI_PROCESSOR_NOTIFY_POWER:
  673. acpi_processor_cst_has_changed(pr);
  674. acpi_bus_generate_proc_event(device, event, 0);
  675. acpi_bus_generate_netlink_event(device->pnp.device_class,
  676. dev_name(&device->dev), event, 0);
  677. break;
  678. case ACPI_PROCESSOR_NOTIFY_THROTTLING:
  679. acpi_processor_tstate_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. default:
  684. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  685. "Unsupported event [0x%x]\n", event));
  686. break;
  687. }
  688. return;
  689. }
  690. static int acpi_cpu_soft_notify(struct notifier_block *nfb,
  691. unsigned long action, void *hcpu)
  692. {
  693. unsigned int cpu = (unsigned long)hcpu;
  694. struct acpi_processor *pr = per_cpu(processors, cpu);
  695. if (action == CPU_ONLINE && pr) {
  696. acpi_processor_ppc_has_changed(pr);
  697. acpi_processor_cst_has_changed(pr);
  698. acpi_processor_tstate_has_changed(pr);
  699. }
  700. return NOTIFY_OK;
  701. }
  702. static struct notifier_block acpi_cpu_notifier =
  703. {
  704. .notifier_call = acpi_cpu_soft_notify,
  705. };
  706. static int acpi_processor_add(struct acpi_device *device)
  707. {
  708. struct acpi_processor *pr = NULL;
  709. if (!device)
  710. return -EINVAL;
  711. pr = kzalloc(sizeof(struct acpi_processor), GFP_KERNEL);
  712. if (!pr)
  713. return -ENOMEM;
  714. if (!alloc_cpumask_var(&pr->throttling.shared_cpu_map, GFP_KERNEL)) {
  715. kfree(pr);
  716. return -ENOMEM;
  717. }
  718. pr->handle = device->handle;
  719. strcpy(acpi_device_name(device), ACPI_PROCESSOR_DEVICE_NAME);
  720. strcpy(acpi_device_class(device), ACPI_PROCESSOR_CLASS);
  721. device->driver_data = pr;
  722. return 0;
  723. }
  724. static int acpi_processor_remove(struct acpi_device *device, int type)
  725. {
  726. struct acpi_processor *pr = NULL;
  727. if (!device || !acpi_driver_data(device))
  728. return -EINVAL;
  729. pr = acpi_driver_data(device);
  730. if (pr->id >= nr_cpu_ids)
  731. goto free;
  732. if (type == ACPI_BUS_REMOVAL_EJECT) {
  733. if (acpi_processor_handle_eject(pr))
  734. return -EINVAL;
  735. }
  736. acpi_processor_power_exit(pr, device);
  737. sysfs_remove_link(&device->dev.kobj, "sysdev");
  738. acpi_processor_remove_fs(device);
  739. if (pr->cdev) {
  740. sysfs_remove_link(&device->dev.kobj, "thermal_cooling");
  741. sysfs_remove_link(&pr->cdev->device.kobj, "device");
  742. thermal_cooling_device_unregister(pr->cdev);
  743. pr->cdev = NULL;
  744. }
  745. per_cpu(processors, pr->id) = NULL;
  746. per_cpu(processor_device_array, pr->id) = NULL;
  747. free:
  748. free_cpumask_var(pr->throttling.shared_cpu_map);
  749. kfree(pr);
  750. return 0;
  751. }
  752. #ifdef CONFIG_ACPI_HOTPLUG_CPU
  753. /****************************************************************************
  754. * Acpi processor hotplug support *
  755. ****************************************************************************/
  756. static int is_processor_present(acpi_handle handle)
  757. {
  758. acpi_status status;
  759. unsigned long long sta = 0;
  760. status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
  761. if (ACPI_SUCCESS(status) && (sta & ACPI_STA_DEVICE_PRESENT))
  762. return 1;
  763. /*
  764. * _STA is mandatory for a processor that supports hot plug
  765. */
  766. if (status == AE_NOT_FOUND)
  767. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  768. "Processor does not support hot plug\n"));
  769. else
  770. ACPI_EXCEPTION((AE_INFO, status,
  771. "Processor Device is not present"));
  772. return 0;
  773. }
  774. static
  775. int acpi_processor_device_add(acpi_handle handle, struct acpi_device **device)
  776. {
  777. acpi_handle phandle;
  778. struct acpi_device *pdev;
  779. struct acpi_processor *pr;
  780. if (acpi_get_parent(handle, &phandle)) {
  781. return -ENODEV;
  782. }
  783. if (acpi_bus_get_device(phandle, &pdev)) {
  784. return -ENODEV;
  785. }
  786. if (acpi_bus_add(device, pdev, handle, ACPI_BUS_TYPE_PROCESSOR)) {
  787. return -ENODEV;
  788. }
  789. acpi_bus_start(*device);
  790. pr = acpi_driver_data(*device);
  791. if (!pr)
  792. return -ENODEV;
  793. if ((pr->id >= 0) && (pr->id < nr_cpu_ids)) {
  794. kobject_uevent(&(*device)->dev.kobj, KOBJ_ONLINE);
  795. }
  796. return 0;
  797. }
  798. static void __ref acpi_processor_hotplug_notify(acpi_handle handle,
  799. u32 event, void *data)
  800. {
  801. struct acpi_processor *pr;
  802. struct acpi_device *device = NULL;
  803. int result;
  804. switch (event) {
  805. case ACPI_NOTIFY_BUS_CHECK:
  806. case ACPI_NOTIFY_DEVICE_CHECK:
  807. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  808. "Processor driver received %s event\n",
  809. (event == ACPI_NOTIFY_BUS_CHECK) ?
  810. "ACPI_NOTIFY_BUS_CHECK" : "ACPI_NOTIFY_DEVICE_CHECK"));
  811. if (!is_processor_present(handle))
  812. break;
  813. if (acpi_bus_get_device(handle, &device)) {
  814. result = acpi_processor_device_add(handle, &device);
  815. if (result)
  816. printk(KERN_ERR PREFIX
  817. "Unable to add the device\n");
  818. break;
  819. }
  820. pr = acpi_driver_data(device);
  821. if (!pr) {
  822. printk(KERN_ERR PREFIX "Driver data is NULL\n");
  823. break;
  824. }
  825. if (pr->id >= 0 && (pr->id < nr_cpu_ids)) {
  826. kobject_uevent(&device->dev.kobj, KOBJ_OFFLINE);
  827. break;
  828. }
  829. result = acpi_processor_start(device);
  830. if ((!result) && ((pr->id >= 0) && (pr->id < nr_cpu_ids))) {
  831. kobject_uevent(&device->dev.kobj, KOBJ_ONLINE);
  832. } else {
  833. printk(KERN_ERR PREFIX "Device [%s] failed to start\n",
  834. acpi_device_bid(device));
  835. }
  836. break;
  837. case ACPI_NOTIFY_EJECT_REQUEST:
  838. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  839. "received ACPI_NOTIFY_EJECT_REQUEST\n"));
  840. if (acpi_bus_get_device(handle, &device)) {
  841. printk(KERN_ERR PREFIX
  842. "Device don't exist, dropping EJECT\n");
  843. break;
  844. }
  845. pr = acpi_driver_data(device);
  846. if (!pr) {
  847. printk(KERN_ERR PREFIX
  848. "Driver data is NULL, dropping EJECT\n");
  849. return;
  850. }
  851. if ((pr->id < nr_cpu_ids) && (cpu_present(pr->id)))
  852. kobject_uevent(&device->dev.kobj, KOBJ_OFFLINE);
  853. break;
  854. default:
  855. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  856. "Unsupported event [0x%x]\n", event));
  857. break;
  858. }
  859. return;
  860. }
  861. static acpi_status
  862. processor_walk_namespace_cb(acpi_handle handle,
  863. u32 lvl, void *context, void **rv)
  864. {
  865. acpi_status status;
  866. int *action = context;
  867. acpi_object_type type = 0;
  868. status = acpi_get_type(handle, &type);
  869. if (ACPI_FAILURE(status))
  870. return (AE_OK);
  871. if (type != ACPI_TYPE_PROCESSOR)
  872. return (AE_OK);
  873. switch (*action) {
  874. case INSTALL_NOTIFY_HANDLER:
  875. acpi_install_notify_handler(handle,
  876. ACPI_SYSTEM_NOTIFY,
  877. acpi_processor_hotplug_notify,
  878. NULL);
  879. break;
  880. case UNINSTALL_NOTIFY_HANDLER:
  881. acpi_remove_notify_handler(handle,
  882. ACPI_SYSTEM_NOTIFY,
  883. acpi_processor_hotplug_notify);
  884. break;
  885. default:
  886. break;
  887. }
  888. return (AE_OK);
  889. }
  890. static acpi_status acpi_processor_hotadd_init(acpi_handle handle, int *p_cpu)
  891. {
  892. if (!is_processor_present(handle)) {
  893. return AE_ERROR;
  894. }
  895. if (acpi_map_lsapic(handle, p_cpu))
  896. return AE_ERROR;
  897. if (arch_register_cpu(*p_cpu)) {
  898. acpi_unmap_lsapic(*p_cpu);
  899. return AE_ERROR;
  900. }
  901. return AE_OK;
  902. }
  903. static int acpi_processor_handle_eject(struct acpi_processor *pr)
  904. {
  905. if (cpu_online(pr->id))
  906. cpu_down(pr->id);
  907. arch_unregister_cpu(pr->id);
  908. acpi_unmap_lsapic(pr->id);
  909. return (0);
  910. }
  911. #else
  912. static acpi_status acpi_processor_hotadd_init(acpi_handle handle, int *p_cpu)
  913. {
  914. return AE_ERROR;
  915. }
  916. static int acpi_processor_handle_eject(struct acpi_processor *pr)
  917. {
  918. return (-EINVAL);
  919. }
  920. #endif
  921. static
  922. void acpi_processor_install_hotplug_notify(void)
  923. {
  924. #ifdef CONFIG_ACPI_HOTPLUG_CPU
  925. int action = INSTALL_NOTIFY_HANDLER;
  926. acpi_walk_namespace(ACPI_TYPE_PROCESSOR,
  927. ACPI_ROOT_OBJECT,
  928. ACPI_UINT32_MAX,
  929. processor_walk_namespace_cb, &action, NULL);
  930. #endif
  931. register_hotcpu_notifier(&acpi_cpu_notifier);
  932. }
  933. static
  934. void acpi_processor_uninstall_hotplug_notify(void)
  935. {
  936. #ifdef CONFIG_ACPI_HOTPLUG_CPU
  937. int action = UNINSTALL_NOTIFY_HANDLER;
  938. acpi_walk_namespace(ACPI_TYPE_PROCESSOR,
  939. ACPI_ROOT_OBJECT,
  940. ACPI_UINT32_MAX,
  941. processor_walk_namespace_cb, &action, NULL);
  942. #endif
  943. unregister_hotcpu_notifier(&acpi_cpu_notifier);
  944. }
  945. /*
  946. * We keep the driver loaded even when ACPI is not running.
  947. * This is needed for the powernow-k8 driver, that works even without
  948. * ACPI, but needs symbols from this driver
  949. */
  950. static int __init acpi_processor_init(void)
  951. {
  952. int result = 0;
  953. memset(&errata, 0, sizeof(errata));
  954. #ifdef CONFIG_SMP
  955. if (ACPI_FAILURE(acpi_get_table(ACPI_SIG_MADT, 0,
  956. (struct acpi_table_header **)&madt)))
  957. madt = NULL;
  958. #endif
  959. acpi_processor_dir = proc_mkdir(ACPI_PROCESSOR_CLASS, acpi_root_dir);
  960. if (!acpi_processor_dir)
  961. return -ENOMEM;
  962. /*
  963. * Check whether the system is DMI table. If yes, OSPM
  964. * should not use mwait for CPU-states.
  965. */
  966. dmi_check_system(processor_idle_dmi_table);
  967. result = cpuidle_register_driver(&acpi_idle_driver);
  968. if (result < 0)
  969. goto out_proc;
  970. result = acpi_bus_register_driver(&acpi_processor_driver);
  971. if (result < 0)
  972. goto out_cpuidle;
  973. acpi_processor_install_hotplug_notify();
  974. acpi_thermal_cpufreq_init();
  975. acpi_processor_ppc_init();
  976. acpi_processor_throttling_init();
  977. return 0;
  978. out_cpuidle:
  979. cpuidle_unregister_driver(&acpi_idle_driver);
  980. out_proc:
  981. remove_proc_entry(ACPI_PROCESSOR_CLASS, acpi_root_dir);
  982. return result;
  983. }
  984. static void __exit acpi_processor_exit(void)
  985. {
  986. acpi_processor_ppc_exit();
  987. acpi_thermal_cpufreq_exit();
  988. acpi_processor_uninstall_hotplug_notify();
  989. acpi_bus_unregister_driver(&acpi_processor_driver);
  990. cpuidle_unregister_driver(&acpi_idle_driver);
  991. remove_proc_entry(ACPI_PROCESSOR_CLASS, acpi_root_dir);
  992. return;
  993. }
  994. module_init(acpi_processor_init);
  995. module_exit(acpi_processor_exit);
  996. EXPORT_SYMBOL(acpi_processor_set_thermal_limit);
  997. MODULE_ALIAS("processor");