processor_core.c 29 KB

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