osl.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322
  1. /*
  2. * acpi_osl.c - OS-dependent functions ($Revision: 83 $)
  3. *
  4. * Copyright (C) 2000 Andrew Henroid
  5. * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
  6. * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
  7. *
  8. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  25. *
  26. */
  27. #include <linux/module.h>
  28. #include <linux/kernel.h>
  29. #include <linux/slab.h>
  30. #include <linux/mm.h>
  31. #include <linux/pci.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/kmod.h>
  34. #include <linux/delay.h>
  35. #include <linux/dmi.h>
  36. #include <linux/workqueue.h>
  37. #include <linux/nmi.h>
  38. #include <linux/acpi.h>
  39. #include <acpi/acpi.h>
  40. #include <asm/io.h>
  41. #include <acpi/acpi_bus.h>
  42. #include <acpi/processor.h>
  43. #include <asm/uaccess.h>
  44. #include <linux/efi.h>
  45. #define _COMPONENT ACPI_OS_SERVICES
  46. ACPI_MODULE_NAME("osl");
  47. #define PREFIX "ACPI: "
  48. struct acpi_os_dpc {
  49. acpi_osd_exec_callback function;
  50. void *context;
  51. struct work_struct work;
  52. };
  53. #ifdef CONFIG_ACPI_CUSTOM_DSDT
  54. #include CONFIG_ACPI_CUSTOM_DSDT_FILE
  55. #endif
  56. #ifdef ENABLE_DEBUGGER
  57. #include <linux/kdb.h>
  58. /* stuff for debugger support */
  59. int acpi_in_debugger;
  60. EXPORT_SYMBOL(acpi_in_debugger);
  61. extern char line_buf[80];
  62. #endif /*ENABLE_DEBUGGER */
  63. static unsigned int acpi_irq_irq;
  64. static acpi_osd_handler acpi_irq_handler;
  65. static void *acpi_irq_context;
  66. static struct workqueue_struct *kacpid_wq;
  67. static struct workqueue_struct *kacpi_notify_wq;
  68. #define OSI_STRING_LENGTH_MAX 64 /* arbitrary */
  69. static char osi_additional_string[OSI_STRING_LENGTH_MAX];
  70. /*
  71. * "Ode to _OSI(Linux)"
  72. *
  73. * osi_linux -- Control response to BIOS _OSI(Linux) query.
  74. *
  75. * As Linux evolves, the features that it supports change.
  76. * So an OSI string such as "Linux" is not specific enough
  77. * to be useful across multiple versions of Linux. It
  78. * doesn't identify any particular feature, interface,
  79. * or even any particular version of Linux...
  80. *
  81. * Unfortunately, Linux-2.6.22 and earlier responded "yes"
  82. * to a BIOS _OSI(Linux) query. When
  83. * a reference mobile BIOS started using it, its use
  84. * started to spread to many vendor platforms.
  85. * As it is not supportable, we need to halt that spread.
  86. *
  87. * Today, most BIOS references to _OSI(Linux) are noise --
  88. * they have no functional effect and are just dead code
  89. * carried over from the reference BIOS.
  90. *
  91. * The next most common case is that _OSI(Linux) harms Linux,
  92. * usually by causing the BIOS to follow paths that are
  93. * not tested during Windows validation.
  94. *
  95. * Finally, there is a short list of platforms
  96. * where OSI(Linux) benefits Linux.
  97. *
  98. * In Linux-2.6.23, OSI(Linux) is first disabled by default.
  99. * DMI is used to disable the dmesg warning about OSI(Linux)
  100. * on platforms where it is known to have no effect.
  101. * But a dmesg warning remains for systems where
  102. * we do not know if OSI(Linux) is good or bad for the system.
  103. * DMI is also used to enable OSI(Linux) for the machines
  104. * that are known to need it.
  105. *
  106. * BIOS writers should NOT query _OSI(Linux) on future systems.
  107. * It will be ignored by default, and to get Linux to
  108. * not ignore it will require a kernel source update to
  109. * add a DMI entry, or a boot-time "acpi_osi=Linux" invocation.
  110. */
  111. #define OSI_LINUX_ENABLE 0
  112. struct osi_linux {
  113. unsigned int enable:1;
  114. unsigned int dmi:1;
  115. unsigned int cmdline:1;
  116. unsigned int known:1;
  117. } osi_linux = { OSI_LINUX_ENABLE, 0, 0, 0};
  118. static void __init acpi_request_region (struct acpi_generic_address *addr,
  119. unsigned int length, char *desc)
  120. {
  121. struct resource *res;
  122. if (!addr->address || !length)
  123. return;
  124. if (addr->space_id == ACPI_ADR_SPACE_SYSTEM_IO)
  125. res = request_region(addr->address, length, desc);
  126. else if (addr->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
  127. res = request_mem_region(addr->address, length, desc);
  128. }
  129. static int __init acpi_reserve_resources(void)
  130. {
  131. acpi_request_region(&acpi_gbl_FADT.xpm1a_event_block, acpi_gbl_FADT.pm1_event_length,
  132. "ACPI PM1a_EVT_BLK");
  133. acpi_request_region(&acpi_gbl_FADT.xpm1b_event_block, acpi_gbl_FADT.pm1_event_length,
  134. "ACPI PM1b_EVT_BLK");
  135. acpi_request_region(&acpi_gbl_FADT.xpm1a_control_block, acpi_gbl_FADT.pm1_control_length,
  136. "ACPI PM1a_CNT_BLK");
  137. acpi_request_region(&acpi_gbl_FADT.xpm1b_control_block, acpi_gbl_FADT.pm1_control_length,
  138. "ACPI PM1b_CNT_BLK");
  139. if (acpi_gbl_FADT.pm_timer_length == 4)
  140. acpi_request_region(&acpi_gbl_FADT.xpm_timer_block, 4, "ACPI PM_TMR");
  141. acpi_request_region(&acpi_gbl_FADT.xpm2_control_block, acpi_gbl_FADT.pm2_control_length,
  142. "ACPI PM2_CNT_BLK");
  143. /* Length of GPE blocks must be a non-negative multiple of 2 */
  144. if (!(acpi_gbl_FADT.gpe0_block_length & 0x1))
  145. acpi_request_region(&acpi_gbl_FADT.xgpe0_block,
  146. acpi_gbl_FADT.gpe0_block_length, "ACPI GPE0_BLK");
  147. if (!(acpi_gbl_FADT.gpe1_block_length & 0x1))
  148. acpi_request_region(&acpi_gbl_FADT.xgpe1_block,
  149. acpi_gbl_FADT.gpe1_block_length, "ACPI GPE1_BLK");
  150. return 0;
  151. }
  152. device_initcall(acpi_reserve_resources);
  153. acpi_status __init acpi_os_initialize(void)
  154. {
  155. return AE_OK;
  156. }
  157. acpi_status acpi_os_initialize1(void)
  158. {
  159. /*
  160. * Initialize PCI configuration space access, as we'll need to access
  161. * it while walking the namespace (bus 0 and root bridges w/ _BBNs).
  162. */
  163. if (!raw_pci_ops) {
  164. printk(KERN_ERR PREFIX
  165. "Access to PCI configuration space unavailable\n");
  166. return AE_NULL_ENTRY;
  167. }
  168. kacpid_wq = create_singlethread_workqueue("kacpid");
  169. kacpi_notify_wq = create_singlethread_workqueue("kacpi_notify");
  170. BUG_ON(!kacpid_wq);
  171. BUG_ON(!kacpi_notify_wq);
  172. return AE_OK;
  173. }
  174. acpi_status acpi_os_terminate(void)
  175. {
  176. if (acpi_irq_handler) {
  177. acpi_os_remove_interrupt_handler(acpi_irq_irq,
  178. acpi_irq_handler);
  179. }
  180. destroy_workqueue(kacpid_wq);
  181. destroy_workqueue(kacpi_notify_wq);
  182. return AE_OK;
  183. }
  184. void acpi_os_printf(const char *fmt, ...)
  185. {
  186. va_list args;
  187. va_start(args, fmt);
  188. acpi_os_vprintf(fmt, args);
  189. va_end(args);
  190. }
  191. EXPORT_SYMBOL(acpi_os_printf);
  192. void acpi_os_vprintf(const char *fmt, va_list args)
  193. {
  194. static char buffer[512];
  195. vsprintf(buffer, fmt, args);
  196. #ifdef ENABLE_DEBUGGER
  197. if (acpi_in_debugger) {
  198. kdb_printf("%s", buffer);
  199. } else {
  200. printk("%s", buffer);
  201. }
  202. #else
  203. printk("%s", buffer);
  204. #endif
  205. }
  206. acpi_physical_address __init acpi_os_get_root_pointer(void)
  207. {
  208. if (efi_enabled) {
  209. if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
  210. return efi.acpi20;
  211. else if (efi.acpi != EFI_INVALID_TABLE_ADDR)
  212. return efi.acpi;
  213. else {
  214. printk(KERN_ERR PREFIX
  215. "System description tables not found\n");
  216. return 0;
  217. }
  218. } else
  219. return acpi_find_rsdp();
  220. }
  221. void __iomem *acpi_os_map_memory(acpi_physical_address phys, acpi_size size)
  222. {
  223. if (phys > ULONG_MAX) {
  224. printk(KERN_ERR PREFIX "Cannot map memory that high\n");
  225. return NULL;
  226. }
  227. if (acpi_gbl_permanent_mmap)
  228. /*
  229. * ioremap checks to ensure this is in reserved space
  230. */
  231. return ioremap((unsigned long)phys, size);
  232. else
  233. return __acpi_map_table((unsigned long)phys, size);
  234. }
  235. EXPORT_SYMBOL_GPL(acpi_os_map_memory);
  236. void acpi_os_unmap_memory(void __iomem * virt, acpi_size size)
  237. {
  238. if (acpi_gbl_permanent_mmap) {
  239. iounmap(virt);
  240. }
  241. }
  242. EXPORT_SYMBOL_GPL(acpi_os_unmap_memory);
  243. #ifdef ACPI_FUTURE_USAGE
  244. acpi_status
  245. acpi_os_get_physical_address(void *virt, acpi_physical_address * phys)
  246. {
  247. if (!phys || !virt)
  248. return AE_BAD_PARAMETER;
  249. *phys = virt_to_phys(virt);
  250. return AE_OK;
  251. }
  252. #endif
  253. #define ACPI_MAX_OVERRIDE_LEN 100
  254. static char acpi_os_name[ACPI_MAX_OVERRIDE_LEN];
  255. acpi_status
  256. acpi_os_predefined_override(const struct acpi_predefined_names *init_val,
  257. acpi_string * new_val)
  258. {
  259. if (!init_val || !new_val)
  260. return AE_BAD_PARAMETER;
  261. *new_val = NULL;
  262. if (!memcmp(init_val->name, "_OS_", 4) && strlen(acpi_os_name)) {
  263. printk(KERN_INFO PREFIX "Overriding _OS definition to '%s'\n",
  264. acpi_os_name);
  265. *new_val = acpi_os_name;
  266. }
  267. return AE_OK;
  268. }
  269. acpi_status
  270. acpi_os_table_override(struct acpi_table_header * existing_table,
  271. struct acpi_table_header ** new_table)
  272. {
  273. if (!existing_table || !new_table)
  274. return AE_BAD_PARAMETER;
  275. #ifdef CONFIG_ACPI_CUSTOM_DSDT
  276. if (strncmp(existing_table->signature, "DSDT", 4) == 0)
  277. *new_table = (struct acpi_table_header *)AmlCode;
  278. else
  279. *new_table = NULL;
  280. #else
  281. *new_table = NULL;
  282. #endif
  283. return AE_OK;
  284. }
  285. static irqreturn_t acpi_irq(int irq, void *dev_id)
  286. {
  287. u32 handled;
  288. handled = (*acpi_irq_handler) (acpi_irq_context);
  289. if (handled) {
  290. acpi_irq_handled++;
  291. return IRQ_HANDLED;
  292. } else
  293. return IRQ_NONE;
  294. }
  295. acpi_status
  296. acpi_os_install_interrupt_handler(u32 gsi, acpi_osd_handler handler,
  297. void *context)
  298. {
  299. unsigned int irq;
  300. acpi_irq_stats_init();
  301. /*
  302. * Ignore the GSI from the core, and use the value in our copy of the
  303. * FADT. It may not be the same if an interrupt source override exists
  304. * for the SCI.
  305. */
  306. gsi = acpi_gbl_FADT.sci_interrupt;
  307. if (acpi_gsi_to_irq(gsi, &irq) < 0) {
  308. printk(KERN_ERR PREFIX "SCI (ACPI GSI %d) not registered\n",
  309. gsi);
  310. return AE_OK;
  311. }
  312. acpi_irq_handler = handler;
  313. acpi_irq_context = context;
  314. if (request_irq(irq, acpi_irq, IRQF_SHARED, "acpi", acpi_irq)) {
  315. printk(KERN_ERR PREFIX "SCI (IRQ%d) allocation failed\n", irq);
  316. return AE_NOT_ACQUIRED;
  317. }
  318. acpi_irq_irq = irq;
  319. return AE_OK;
  320. }
  321. acpi_status acpi_os_remove_interrupt_handler(u32 irq, acpi_osd_handler handler)
  322. {
  323. if (irq) {
  324. free_irq(irq, acpi_irq);
  325. acpi_irq_handler = NULL;
  326. acpi_irq_irq = 0;
  327. }
  328. return AE_OK;
  329. }
  330. /*
  331. * Running in interpreter thread context, safe to sleep
  332. */
  333. void acpi_os_sleep(acpi_integer ms)
  334. {
  335. schedule_timeout_interruptible(msecs_to_jiffies(ms));
  336. }
  337. EXPORT_SYMBOL(acpi_os_sleep);
  338. void acpi_os_stall(u32 us)
  339. {
  340. while (us) {
  341. u32 delay = 1000;
  342. if (delay > us)
  343. delay = us;
  344. udelay(delay);
  345. touch_nmi_watchdog();
  346. us -= delay;
  347. }
  348. }
  349. EXPORT_SYMBOL(acpi_os_stall);
  350. /*
  351. * Support ACPI 3.0 AML Timer operand
  352. * Returns 64-bit free-running, monotonically increasing timer
  353. * with 100ns granularity
  354. */
  355. u64 acpi_os_get_timer(void)
  356. {
  357. static u64 t;
  358. #ifdef CONFIG_HPET
  359. /* TBD: use HPET if available */
  360. #endif
  361. #ifdef CONFIG_X86_PM_TIMER
  362. /* TBD: default to PM timer if HPET was not available */
  363. #endif
  364. if (!t)
  365. printk(KERN_ERR PREFIX "acpi_os_get_timer() TBD\n");
  366. return ++t;
  367. }
  368. acpi_status acpi_os_read_port(acpi_io_address port, u32 * value, u32 width)
  369. {
  370. u32 dummy;
  371. if (!value)
  372. value = &dummy;
  373. *value = 0;
  374. if (width <= 8) {
  375. *(u8 *) value = inb(port);
  376. } else if (width <= 16) {
  377. *(u16 *) value = inw(port);
  378. } else if (width <= 32) {
  379. *(u32 *) value = inl(port);
  380. } else {
  381. BUG();
  382. }
  383. return AE_OK;
  384. }
  385. EXPORT_SYMBOL(acpi_os_read_port);
  386. acpi_status acpi_os_write_port(acpi_io_address port, u32 value, u32 width)
  387. {
  388. if (width <= 8) {
  389. outb(value, port);
  390. } else if (width <= 16) {
  391. outw(value, port);
  392. } else if (width <= 32) {
  393. outl(value, port);
  394. } else {
  395. BUG();
  396. }
  397. return AE_OK;
  398. }
  399. EXPORT_SYMBOL(acpi_os_write_port);
  400. acpi_status
  401. acpi_os_read_memory(acpi_physical_address phys_addr, u32 * value, u32 width)
  402. {
  403. u32 dummy;
  404. void __iomem *virt_addr;
  405. virt_addr = ioremap(phys_addr, width);
  406. if (!value)
  407. value = &dummy;
  408. switch (width) {
  409. case 8:
  410. *(u8 *) value = readb(virt_addr);
  411. break;
  412. case 16:
  413. *(u16 *) value = readw(virt_addr);
  414. break;
  415. case 32:
  416. *(u32 *) value = readl(virt_addr);
  417. break;
  418. default:
  419. BUG();
  420. }
  421. iounmap(virt_addr);
  422. return AE_OK;
  423. }
  424. acpi_status
  425. acpi_os_write_memory(acpi_physical_address phys_addr, u32 value, u32 width)
  426. {
  427. void __iomem *virt_addr;
  428. virt_addr = ioremap(phys_addr, width);
  429. switch (width) {
  430. case 8:
  431. writeb(value, virt_addr);
  432. break;
  433. case 16:
  434. writew(value, virt_addr);
  435. break;
  436. case 32:
  437. writel(value, virt_addr);
  438. break;
  439. default:
  440. BUG();
  441. }
  442. iounmap(virt_addr);
  443. return AE_OK;
  444. }
  445. acpi_status
  446. acpi_os_read_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
  447. void *value, u32 width)
  448. {
  449. int result, size;
  450. if (!value)
  451. return AE_BAD_PARAMETER;
  452. switch (width) {
  453. case 8:
  454. size = 1;
  455. break;
  456. case 16:
  457. size = 2;
  458. break;
  459. case 32:
  460. size = 4;
  461. break;
  462. default:
  463. return AE_ERROR;
  464. }
  465. BUG_ON(!raw_pci_ops);
  466. result = raw_pci_ops->read(pci_id->segment, pci_id->bus,
  467. PCI_DEVFN(pci_id->device, pci_id->function),
  468. reg, size, value);
  469. return (result ? AE_ERROR : AE_OK);
  470. }
  471. EXPORT_SYMBOL(acpi_os_read_pci_configuration);
  472. acpi_status
  473. acpi_os_write_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
  474. acpi_integer value, u32 width)
  475. {
  476. int result, size;
  477. switch (width) {
  478. case 8:
  479. size = 1;
  480. break;
  481. case 16:
  482. size = 2;
  483. break;
  484. case 32:
  485. size = 4;
  486. break;
  487. default:
  488. return AE_ERROR;
  489. }
  490. BUG_ON(!raw_pci_ops);
  491. result = raw_pci_ops->write(pci_id->segment, pci_id->bus,
  492. PCI_DEVFN(pci_id->device, pci_id->function),
  493. reg, size, value);
  494. return (result ? AE_ERROR : AE_OK);
  495. }
  496. /* TODO: Change code to take advantage of driver model more */
  497. static void acpi_os_derive_pci_id_2(acpi_handle rhandle, /* upper bound */
  498. acpi_handle chandle, /* current node */
  499. struct acpi_pci_id **id,
  500. int *is_bridge, u8 * bus_number)
  501. {
  502. acpi_handle handle;
  503. struct acpi_pci_id *pci_id = *id;
  504. acpi_status status;
  505. unsigned long temp;
  506. acpi_object_type type;
  507. u8 tu8;
  508. acpi_get_parent(chandle, &handle);
  509. if (handle != rhandle) {
  510. acpi_os_derive_pci_id_2(rhandle, handle, &pci_id, is_bridge,
  511. bus_number);
  512. status = acpi_get_type(handle, &type);
  513. if ((ACPI_FAILURE(status)) || (type != ACPI_TYPE_DEVICE))
  514. return;
  515. status =
  516. acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL,
  517. &temp);
  518. if (ACPI_SUCCESS(status)) {
  519. pci_id->device = ACPI_HIWORD(ACPI_LODWORD(temp));
  520. pci_id->function = ACPI_LOWORD(ACPI_LODWORD(temp));
  521. if (*is_bridge)
  522. pci_id->bus = *bus_number;
  523. /* any nicer way to get bus number of bridge ? */
  524. status =
  525. acpi_os_read_pci_configuration(pci_id, 0x0e, &tu8,
  526. 8);
  527. if (ACPI_SUCCESS(status)
  528. && ((tu8 & 0x7f) == 1 || (tu8 & 0x7f) == 2)) {
  529. status =
  530. acpi_os_read_pci_configuration(pci_id, 0x18,
  531. &tu8, 8);
  532. if (!ACPI_SUCCESS(status)) {
  533. /* Certainly broken... FIX ME */
  534. return;
  535. }
  536. *is_bridge = 1;
  537. pci_id->bus = tu8;
  538. status =
  539. acpi_os_read_pci_configuration(pci_id, 0x19,
  540. &tu8, 8);
  541. if (ACPI_SUCCESS(status)) {
  542. *bus_number = tu8;
  543. }
  544. } else
  545. *is_bridge = 0;
  546. }
  547. }
  548. }
  549. void acpi_os_derive_pci_id(acpi_handle rhandle, /* upper bound */
  550. acpi_handle chandle, /* current node */
  551. struct acpi_pci_id **id)
  552. {
  553. int is_bridge = 1;
  554. u8 bus_number = (*id)->bus;
  555. acpi_os_derive_pci_id_2(rhandle, chandle, id, &is_bridge, &bus_number);
  556. }
  557. static void acpi_os_execute_deferred(struct work_struct *work)
  558. {
  559. struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
  560. if (!dpc) {
  561. printk(KERN_ERR PREFIX "Invalid (NULL) context\n");
  562. return;
  563. }
  564. dpc->function(dpc->context);
  565. kfree(dpc);
  566. /* Yield cpu to notify thread */
  567. cond_resched();
  568. return;
  569. }
  570. static void acpi_os_execute_notify(struct work_struct *work)
  571. {
  572. struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
  573. if (!dpc) {
  574. printk(KERN_ERR PREFIX "Invalid (NULL) context\n");
  575. return;
  576. }
  577. dpc->function(dpc->context);
  578. kfree(dpc);
  579. return;
  580. }
  581. /*******************************************************************************
  582. *
  583. * FUNCTION: acpi_os_execute
  584. *
  585. * PARAMETERS: Type - Type of the callback
  586. * Function - Function to be executed
  587. * Context - Function parameters
  588. *
  589. * RETURN: Status
  590. *
  591. * DESCRIPTION: Depending on type, either queues function for deferred execution or
  592. * immediately executes function on a separate thread.
  593. *
  594. ******************************************************************************/
  595. acpi_status acpi_os_execute(acpi_execute_type type,
  596. acpi_osd_exec_callback function, void *context)
  597. {
  598. acpi_status status = AE_OK;
  599. struct acpi_os_dpc *dpc;
  600. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  601. "Scheduling function [%p(%p)] for deferred execution.\n",
  602. function, context));
  603. if (!function)
  604. return AE_BAD_PARAMETER;
  605. /*
  606. * Allocate/initialize DPC structure. Note that this memory will be
  607. * freed by the callee. The kernel handles the work_struct list in a
  608. * way that allows us to also free its memory inside the callee.
  609. * Because we may want to schedule several tasks with different
  610. * parameters we can't use the approach some kernel code uses of
  611. * having a static work_struct.
  612. */
  613. dpc = kmalloc(sizeof(struct acpi_os_dpc), GFP_ATOMIC);
  614. if (!dpc)
  615. return_ACPI_STATUS(AE_NO_MEMORY);
  616. dpc->function = function;
  617. dpc->context = context;
  618. if (type == OSL_NOTIFY_HANDLER) {
  619. INIT_WORK(&dpc->work, acpi_os_execute_notify);
  620. if (!queue_work(kacpi_notify_wq, &dpc->work)) {
  621. status = AE_ERROR;
  622. kfree(dpc);
  623. }
  624. } else {
  625. INIT_WORK(&dpc->work, acpi_os_execute_deferred);
  626. if (!queue_work(kacpid_wq, &dpc->work)) {
  627. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  628. "Call to queue_work() failed.\n"));
  629. status = AE_ERROR;
  630. kfree(dpc);
  631. }
  632. }
  633. return_ACPI_STATUS(status);
  634. }
  635. EXPORT_SYMBOL(acpi_os_execute);
  636. void acpi_os_wait_events_complete(void *context)
  637. {
  638. flush_workqueue(kacpid_wq);
  639. }
  640. EXPORT_SYMBOL(acpi_os_wait_events_complete);
  641. /*
  642. * Allocate the memory for a spinlock and initialize it.
  643. */
  644. acpi_status acpi_os_create_lock(acpi_spinlock * handle)
  645. {
  646. spin_lock_init(*handle);
  647. return AE_OK;
  648. }
  649. /*
  650. * Deallocate the memory for a spinlock.
  651. */
  652. void acpi_os_delete_lock(acpi_spinlock handle)
  653. {
  654. return;
  655. }
  656. acpi_status
  657. acpi_os_create_semaphore(u32 max_units, u32 initial_units, acpi_handle * handle)
  658. {
  659. struct semaphore *sem = NULL;
  660. sem = acpi_os_allocate(sizeof(struct semaphore));
  661. if (!sem)
  662. return AE_NO_MEMORY;
  663. memset(sem, 0, sizeof(struct semaphore));
  664. sema_init(sem, initial_units);
  665. *handle = (acpi_handle *) sem;
  666. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n",
  667. *handle, initial_units));
  668. return AE_OK;
  669. }
  670. EXPORT_SYMBOL(acpi_os_create_semaphore);
  671. /*
  672. * TODO: A better way to delete semaphores? Linux doesn't have a
  673. * 'delete_semaphore()' function -- may result in an invalid
  674. * pointer dereference for non-synchronized consumers. Should
  675. * we at least check for blocked threads and signal/cancel them?
  676. */
  677. acpi_status acpi_os_delete_semaphore(acpi_handle handle)
  678. {
  679. struct semaphore *sem = (struct semaphore *)handle;
  680. if (!sem)
  681. return AE_BAD_PARAMETER;
  682. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
  683. kfree(sem);
  684. sem = NULL;
  685. return AE_OK;
  686. }
  687. EXPORT_SYMBOL(acpi_os_delete_semaphore);
  688. /*
  689. * TODO: The kernel doesn't have a 'down_timeout' function -- had to
  690. * improvise. The process is to sleep for one scheduler quantum
  691. * until the semaphore becomes available. Downside is that this
  692. * may result in starvation for timeout-based waits when there's
  693. * lots of semaphore activity.
  694. *
  695. * TODO: Support for units > 1?
  696. */
  697. acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
  698. {
  699. acpi_status status = AE_OK;
  700. struct semaphore *sem = (struct semaphore *)handle;
  701. int ret = 0;
  702. if (!sem || (units < 1))
  703. return AE_BAD_PARAMETER;
  704. if (units > 1)
  705. return AE_SUPPORT;
  706. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n",
  707. handle, units, timeout));
  708. /*
  709. * This can be called during resume with interrupts off.
  710. * Like boot-time, we should be single threaded and will
  711. * always get the lock if we try -- timeout or not.
  712. * If this doesn't succeed, then we will oops courtesy of
  713. * might_sleep() in down().
  714. */
  715. if (!down_trylock(sem))
  716. return AE_OK;
  717. switch (timeout) {
  718. /*
  719. * No Wait:
  720. * --------
  721. * A zero timeout value indicates that we shouldn't wait - just
  722. * acquire the semaphore if available otherwise return AE_TIME
  723. * (a.k.a. 'would block').
  724. */
  725. case 0:
  726. if (down_trylock(sem))
  727. status = AE_TIME;
  728. break;
  729. /*
  730. * Wait Indefinitely:
  731. * ------------------
  732. */
  733. case ACPI_WAIT_FOREVER:
  734. down(sem);
  735. break;
  736. /*
  737. * Wait w/ Timeout:
  738. * ----------------
  739. */
  740. default:
  741. // TODO: A better timeout algorithm?
  742. {
  743. int i = 0;
  744. static const int quantum_ms = 1000 / HZ;
  745. ret = down_trylock(sem);
  746. for (i = timeout; (i > 0 && ret != 0); i -= quantum_ms) {
  747. schedule_timeout_interruptible(1);
  748. ret = down_trylock(sem);
  749. }
  750. if (ret != 0)
  751. status = AE_TIME;
  752. }
  753. break;
  754. }
  755. if (ACPI_FAILURE(status)) {
  756. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
  757. "Failed to acquire semaphore[%p|%d|%d], %s",
  758. handle, units, timeout,
  759. acpi_format_exception(status)));
  760. } else {
  761. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
  762. "Acquired semaphore[%p|%d|%d]", handle,
  763. units, timeout));
  764. }
  765. return status;
  766. }
  767. EXPORT_SYMBOL(acpi_os_wait_semaphore);
  768. /*
  769. * TODO: Support for units > 1?
  770. */
  771. acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
  772. {
  773. struct semaphore *sem = (struct semaphore *)handle;
  774. if (!sem || (units < 1))
  775. return AE_BAD_PARAMETER;
  776. if (units > 1)
  777. return AE_SUPPORT;
  778. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle,
  779. units));
  780. up(sem);
  781. return AE_OK;
  782. }
  783. EXPORT_SYMBOL(acpi_os_signal_semaphore);
  784. #ifdef ACPI_FUTURE_USAGE
  785. u32 acpi_os_get_line(char *buffer)
  786. {
  787. #ifdef ENABLE_DEBUGGER
  788. if (acpi_in_debugger) {
  789. u32 chars;
  790. kdb_read(buffer, sizeof(line_buf));
  791. /* remove the CR kdb includes */
  792. chars = strlen(buffer) - 1;
  793. buffer[chars] = '\0';
  794. }
  795. #endif
  796. return 0;
  797. }
  798. #endif /* ACPI_FUTURE_USAGE */
  799. acpi_status acpi_os_signal(u32 function, void *info)
  800. {
  801. switch (function) {
  802. case ACPI_SIGNAL_FATAL:
  803. printk(KERN_ERR PREFIX "Fatal opcode executed\n");
  804. break;
  805. case ACPI_SIGNAL_BREAKPOINT:
  806. /*
  807. * AML Breakpoint
  808. * ACPI spec. says to treat it as a NOP unless
  809. * you are debugging. So if/when we integrate
  810. * AML debugger into the kernel debugger its
  811. * hook will go here. But until then it is
  812. * not useful to print anything on breakpoints.
  813. */
  814. break;
  815. default:
  816. break;
  817. }
  818. return AE_OK;
  819. }
  820. EXPORT_SYMBOL(acpi_os_signal);
  821. static int __init acpi_os_name_setup(char *str)
  822. {
  823. char *p = acpi_os_name;
  824. int count = ACPI_MAX_OVERRIDE_LEN - 1;
  825. if (!str || !*str)
  826. return 0;
  827. for (; count-- && str && *str; str++) {
  828. if (isalnum(*str) || *str == ' ' || *str == ':')
  829. *p++ = *str;
  830. else if (*str == '\'' || *str == '"')
  831. continue;
  832. else
  833. break;
  834. }
  835. *p = 0;
  836. return 1;
  837. }
  838. __setup("acpi_os_name=", acpi_os_name_setup);
  839. static void __init set_osi_linux(unsigned int enable)
  840. {
  841. if (osi_linux.enable != enable) {
  842. osi_linux.enable = enable;
  843. printk(KERN_NOTICE PREFIX "%sed _OSI(Linux)\n",
  844. enable ? "Add": "Delet");
  845. }
  846. return;
  847. }
  848. static void __init acpi_cmdline_osi_linux(unsigned int enable)
  849. {
  850. osi_linux.cmdline = 1; /* cmdline set the default */
  851. set_osi_linux(enable);
  852. return;
  853. }
  854. void __init acpi_dmi_osi_linux(int enable, const struct dmi_system_id *d)
  855. {
  856. osi_linux.dmi = 1; /* DMI knows that this box asks OSI(Linux) */
  857. printk(KERN_NOTICE PREFIX "DMI detected: %s\n", d->ident);
  858. if (enable == -1)
  859. return;
  860. osi_linux.known = 1; /* DMI knows which OSI(Linux) default needed */
  861. set_osi_linux(enable);
  862. return;
  863. }
  864. /*
  865. * Modify the list of "OS Interfaces" reported to BIOS via _OSI
  866. *
  867. * empty string disables _OSI
  868. * string starting with '!' disables that string
  869. * otherwise string is added to list, augmenting built-in strings
  870. */
  871. static int __init acpi_osi_setup(char *str)
  872. {
  873. if (str == NULL || *str == '\0') {
  874. printk(KERN_INFO PREFIX "_OSI method disabled\n");
  875. acpi_gbl_create_osi_method = FALSE;
  876. } else if (!strcmp("!Linux", str)) {
  877. acpi_cmdline_osi_linux(0); /* !enable */
  878. } else if (*str == '!') {
  879. if (acpi_osi_invalidate(++str) == AE_OK)
  880. printk(KERN_INFO PREFIX "Deleted _OSI(%s)\n", str);
  881. } else if (!strcmp("Linux", str)) {
  882. acpi_cmdline_osi_linux(1); /* enable */
  883. } else if (*osi_additional_string == '\0') {
  884. strncpy(osi_additional_string, str, OSI_STRING_LENGTH_MAX);
  885. printk(KERN_INFO PREFIX "Added _OSI(%s)\n", str);
  886. }
  887. return 1;
  888. }
  889. __setup("acpi_osi=", acpi_osi_setup);
  890. /* enable serialization to combat AE_ALREADY_EXISTS errors */
  891. static int __init acpi_serialize_setup(char *str)
  892. {
  893. printk(KERN_INFO PREFIX "serialize enabled\n");
  894. acpi_gbl_all_methods_serialized = TRUE;
  895. return 1;
  896. }
  897. __setup("acpi_serialize", acpi_serialize_setup);
  898. /*
  899. * Wake and Run-Time GPES are expected to be separate.
  900. * We disable wake-GPEs at run-time to prevent spurious
  901. * interrupts.
  902. *
  903. * However, if a system exists that shares Wake and
  904. * Run-time events on the same GPE this flag is available
  905. * to tell Linux to keep the wake-time GPEs enabled at run-time.
  906. */
  907. static int __init acpi_wake_gpes_always_on_setup(char *str)
  908. {
  909. printk(KERN_INFO PREFIX "wake GPEs not disabled\n");
  910. acpi_gbl_leave_wake_gpes_disabled = FALSE;
  911. return 1;
  912. }
  913. __setup("acpi_wake_gpes_always_on", acpi_wake_gpes_always_on_setup);
  914. /*
  915. * Acquire a spinlock.
  916. *
  917. * handle is a pointer to the spinlock_t.
  918. */
  919. acpi_cpu_flags acpi_os_acquire_lock(acpi_spinlock lockp)
  920. {
  921. acpi_cpu_flags flags;
  922. spin_lock_irqsave(lockp, flags);
  923. return flags;
  924. }
  925. /*
  926. * Release a spinlock. See above.
  927. */
  928. void acpi_os_release_lock(acpi_spinlock lockp, acpi_cpu_flags flags)
  929. {
  930. spin_unlock_irqrestore(lockp, flags);
  931. }
  932. #ifndef ACPI_USE_LOCAL_CACHE
  933. /*******************************************************************************
  934. *
  935. * FUNCTION: acpi_os_create_cache
  936. *
  937. * PARAMETERS: name - Ascii name for the cache
  938. * size - Size of each cached object
  939. * depth - Maximum depth of the cache (in objects) <ignored>
  940. * cache - Where the new cache object is returned
  941. *
  942. * RETURN: status
  943. *
  944. * DESCRIPTION: Create a cache object
  945. *
  946. ******************************************************************************/
  947. acpi_status
  948. acpi_os_create_cache(char *name, u16 size, u16 depth, acpi_cache_t ** cache)
  949. {
  950. *cache = kmem_cache_create(name, size, 0, 0, NULL);
  951. if (*cache == NULL)
  952. return AE_ERROR;
  953. else
  954. return AE_OK;
  955. }
  956. /*******************************************************************************
  957. *
  958. * FUNCTION: acpi_os_purge_cache
  959. *
  960. * PARAMETERS: Cache - Handle to cache object
  961. *
  962. * RETURN: Status
  963. *
  964. * DESCRIPTION: Free all objects within the requested cache.
  965. *
  966. ******************************************************************************/
  967. acpi_status acpi_os_purge_cache(acpi_cache_t * cache)
  968. {
  969. kmem_cache_shrink(cache);
  970. return (AE_OK);
  971. }
  972. /*******************************************************************************
  973. *
  974. * FUNCTION: acpi_os_delete_cache
  975. *
  976. * PARAMETERS: Cache - Handle to cache object
  977. *
  978. * RETURN: Status
  979. *
  980. * DESCRIPTION: Free all objects within the requested cache and delete the
  981. * cache object.
  982. *
  983. ******************************************************************************/
  984. acpi_status acpi_os_delete_cache(acpi_cache_t * cache)
  985. {
  986. kmem_cache_destroy(cache);
  987. return (AE_OK);
  988. }
  989. /*******************************************************************************
  990. *
  991. * FUNCTION: acpi_os_release_object
  992. *
  993. * PARAMETERS: Cache - Handle to cache object
  994. * Object - The object to be released
  995. *
  996. * RETURN: None
  997. *
  998. * DESCRIPTION: Release an object to the specified cache. If cache is full,
  999. * the object is deleted.
  1000. *
  1001. ******************************************************************************/
  1002. acpi_status acpi_os_release_object(acpi_cache_t * cache, void *object)
  1003. {
  1004. kmem_cache_free(cache, object);
  1005. return (AE_OK);
  1006. }
  1007. /**
  1008. * acpi_dmi_dump - dump DMI slots needed for blacklist entry
  1009. *
  1010. * Returns 0 on success
  1011. */
  1012. int acpi_dmi_dump(void)
  1013. {
  1014. if (!dmi_available)
  1015. return -1;
  1016. printk(KERN_NOTICE PREFIX "DMI System Vendor: %s\n",
  1017. dmi_get_slot(DMI_SYS_VENDOR));
  1018. printk(KERN_NOTICE PREFIX "DMI Product Name: %s\n",
  1019. dmi_get_slot(DMI_PRODUCT_NAME));
  1020. printk(KERN_NOTICE PREFIX "DMI Product Version: %s\n",
  1021. dmi_get_slot(DMI_PRODUCT_VERSION));
  1022. printk(KERN_NOTICE PREFIX "DMI Board Name: %s\n",
  1023. dmi_get_slot(DMI_BOARD_NAME));
  1024. printk(KERN_NOTICE PREFIX "DMI BIOS Vendor: %s\n",
  1025. dmi_get_slot(DMI_BIOS_VENDOR));
  1026. printk(KERN_NOTICE PREFIX "DMI BIOS Date: %s\n",
  1027. dmi_get_slot(DMI_BIOS_DATE));
  1028. return 0;
  1029. }
  1030. /******************************************************************************
  1031. *
  1032. * FUNCTION: acpi_os_validate_interface
  1033. *
  1034. * PARAMETERS: interface - Requested interface to be validated
  1035. *
  1036. * RETURN: AE_OK if interface is supported, AE_SUPPORT otherwise
  1037. *
  1038. * DESCRIPTION: Match an interface string to the interfaces supported by the
  1039. * host. Strings originate from an AML call to the _OSI method.
  1040. *
  1041. *****************************************************************************/
  1042. acpi_status
  1043. acpi_os_validate_interface (char *interface)
  1044. {
  1045. if (!strncmp(osi_additional_string, interface, OSI_STRING_LENGTH_MAX))
  1046. return AE_OK;
  1047. if (!strcmp("Linux", interface)) {
  1048. printk(KERN_NOTICE PREFIX
  1049. "BIOS _OSI(Linux) query %s%s\n",
  1050. osi_linux.enable ? "honored" : "ignored",
  1051. osi_linux.cmdline ? " via cmdline" :
  1052. osi_linux.dmi ? " via DMI" : "");
  1053. if (!osi_linux.dmi) {
  1054. if (acpi_dmi_dump())
  1055. printk(KERN_NOTICE PREFIX
  1056. "[please extract dmidecode output]\n");
  1057. printk(KERN_NOTICE PREFIX
  1058. "Please send DMI info above to "
  1059. "linux-acpi@vger.kernel.org\n");
  1060. }
  1061. if (!osi_linux.known && !osi_linux.cmdline) {
  1062. printk(KERN_NOTICE PREFIX
  1063. "If \"acpi_osi=%sLinux\" works better, "
  1064. "please notify linux-acpi@vger.kernel.org\n",
  1065. osi_linux.enable ? "!" : "");
  1066. }
  1067. if (osi_linux.enable)
  1068. return AE_OK;
  1069. }
  1070. return AE_SUPPORT;
  1071. }
  1072. /******************************************************************************
  1073. *
  1074. * FUNCTION: acpi_os_validate_address
  1075. *
  1076. * PARAMETERS: space_id - ACPI space ID
  1077. * address - Physical address
  1078. * length - Address length
  1079. *
  1080. * RETURN: AE_OK if address/length is valid for the space_id. Otherwise,
  1081. * should return AE_AML_ILLEGAL_ADDRESS.
  1082. *
  1083. * DESCRIPTION: Validate a system address via the host OS. Used to validate
  1084. * the addresses accessed by AML operation regions.
  1085. *
  1086. *****************************************************************************/
  1087. acpi_status
  1088. acpi_os_validate_address (
  1089. u8 space_id,
  1090. acpi_physical_address address,
  1091. acpi_size length)
  1092. {
  1093. return AE_OK;
  1094. }
  1095. #endif