processor_core.c 31 KB

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