osl.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316
  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. static 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. acpi_physical_address pa = 0;
  220. acpi_find_root_pointer(&pa);
  221. return pa;
  222. }
  223. }
  224. void __iomem *acpi_os_map_memory(acpi_physical_address phys, acpi_size size)
  225. {
  226. if (phys > ULONG_MAX) {
  227. printk(KERN_ERR PREFIX "Cannot map memory that high\n");
  228. return NULL;
  229. }
  230. if (acpi_gbl_permanent_mmap)
  231. /*
  232. * ioremap checks to ensure this is in reserved space
  233. */
  234. return ioremap((unsigned long)phys, size);
  235. else
  236. return __acpi_map_table((unsigned long)phys, size);
  237. }
  238. EXPORT_SYMBOL_GPL(acpi_os_map_memory);
  239. void acpi_os_unmap_memory(void __iomem * virt, acpi_size size)
  240. {
  241. if (acpi_gbl_permanent_mmap) {
  242. iounmap(virt);
  243. }
  244. }
  245. EXPORT_SYMBOL_GPL(acpi_os_unmap_memory);
  246. #ifdef ACPI_FUTURE_USAGE
  247. acpi_status
  248. acpi_os_get_physical_address(void *virt, acpi_physical_address * phys)
  249. {
  250. if (!phys || !virt)
  251. return AE_BAD_PARAMETER;
  252. *phys = virt_to_phys(virt);
  253. return AE_OK;
  254. }
  255. #endif
  256. #define ACPI_MAX_OVERRIDE_LEN 100
  257. static char acpi_os_name[ACPI_MAX_OVERRIDE_LEN];
  258. acpi_status
  259. acpi_os_predefined_override(const struct acpi_predefined_names *init_val,
  260. acpi_string * new_val)
  261. {
  262. if (!init_val || !new_val)
  263. return AE_BAD_PARAMETER;
  264. *new_val = NULL;
  265. if (!memcmp(init_val->name, "_OS_", 4) && strlen(acpi_os_name)) {
  266. printk(KERN_INFO PREFIX "Overriding _OS definition to '%s'\n",
  267. acpi_os_name);
  268. *new_val = acpi_os_name;
  269. }
  270. return AE_OK;
  271. }
  272. acpi_status
  273. acpi_os_table_override(struct acpi_table_header * existing_table,
  274. struct acpi_table_header ** new_table)
  275. {
  276. if (!existing_table || !new_table)
  277. return AE_BAD_PARAMETER;
  278. #ifdef CONFIG_ACPI_CUSTOM_DSDT
  279. if (strncmp(existing_table->signature, "DSDT", 4) == 0)
  280. *new_table = (struct acpi_table_header *)AmlCode;
  281. else
  282. *new_table = NULL;
  283. #else
  284. *new_table = NULL;
  285. #endif
  286. return AE_OK;
  287. }
  288. static irqreturn_t acpi_irq(int irq, void *dev_id)
  289. {
  290. return (*acpi_irq_handler) (acpi_irq_context) ? IRQ_HANDLED : IRQ_NONE;
  291. }
  292. acpi_status
  293. acpi_os_install_interrupt_handler(u32 gsi, acpi_osd_handler handler,
  294. void *context)
  295. {
  296. unsigned int irq;
  297. /*
  298. * Ignore the GSI from the core, and use the value in our copy of the
  299. * FADT. It may not be the same if an interrupt source override exists
  300. * for the SCI.
  301. */
  302. gsi = acpi_gbl_FADT.sci_interrupt;
  303. if (acpi_gsi_to_irq(gsi, &irq) < 0) {
  304. printk(KERN_ERR PREFIX "SCI (ACPI GSI %d) not registered\n",
  305. gsi);
  306. return AE_OK;
  307. }
  308. acpi_irq_handler = handler;
  309. acpi_irq_context = context;
  310. if (request_irq(irq, acpi_irq, IRQF_SHARED, "acpi", acpi_irq)) {
  311. printk(KERN_ERR PREFIX "SCI (IRQ%d) allocation failed\n", irq);
  312. return AE_NOT_ACQUIRED;
  313. }
  314. acpi_irq_irq = irq;
  315. return AE_OK;
  316. }
  317. acpi_status acpi_os_remove_interrupt_handler(u32 irq, acpi_osd_handler handler)
  318. {
  319. if (irq) {
  320. free_irq(irq, acpi_irq);
  321. acpi_irq_handler = NULL;
  322. acpi_irq_irq = 0;
  323. }
  324. return AE_OK;
  325. }
  326. /*
  327. * Running in interpreter thread context, safe to sleep
  328. */
  329. void acpi_os_sleep(acpi_integer ms)
  330. {
  331. schedule_timeout_interruptible(msecs_to_jiffies(ms));
  332. }
  333. EXPORT_SYMBOL(acpi_os_sleep);
  334. void acpi_os_stall(u32 us)
  335. {
  336. while (us) {
  337. u32 delay = 1000;
  338. if (delay > us)
  339. delay = us;
  340. udelay(delay);
  341. touch_nmi_watchdog();
  342. us -= delay;
  343. }
  344. }
  345. EXPORT_SYMBOL(acpi_os_stall);
  346. /*
  347. * Support ACPI 3.0 AML Timer operand
  348. * Returns 64-bit free-running, monotonically increasing timer
  349. * with 100ns granularity
  350. */
  351. u64 acpi_os_get_timer(void)
  352. {
  353. static u64 t;
  354. #ifdef CONFIG_HPET
  355. /* TBD: use HPET if available */
  356. #endif
  357. #ifdef CONFIG_X86_PM_TIMER
  358. /* TBD: default to PM timer if HPET was not available */
  359. #endif
  360. if (!t)
  361. printk(KERN_ERR PREFIX "acpi_os_get_timer() TBD\n");
  362. return ++t;
  363. }
  364. acpi_status acpi_os_read_port(acpi_io_address port, u32 * value, u32 width)
  365. {
  366. u32 dummy;
  367. if (!value)
  368. value = &dummy;
  369. *value = 0;
  370. if (width <= 8) {
  371. *(u8 *) value = inb(port);
  372. } else if (width <= 16) {
  373. *(u16 *) value = inw(port);
  374. } else if (width <= 32) {
  375. *(u32 *) value = inl(port);
  376. } else {
  377. BUG();
  378. }
  379. return AE_OK;
  380. }
  381. EXPORT_SYMBOL(acpi_os_read_port);
  382. acpi_status acpi_os_write_port(acpi_io_address port, u32 value, u32 width)
  383. {
  384. if (width <= 8) {
  385. outb(value, port);
  386. } else if (width <= 16) {
  387. outw(value, port);
  388. } else if (width <= 32) {
  389. outl(value, port);
  390. } else {
  391. BUG();
  392. }
  393. return AE_OK;
  394. }
  395. EXPORT_SYMBOL(acpi_os_write_port);
  396. acpi_status
  397. acpi_os_read_memory(acpi_physical_address phys_addr, u32 * value, u32 width)
  398. {
  399. u32 dummy;
  400. void __iomem *virt_addr;
  401. virt_addr = ioremap(phys_addr, width);
  402. if (!value)
  403. value = &dummy;
  404. switch (width) {
  405. case 8:
  406. *(u8 *) value = readb(virt_addr);
  407. break;
  408. case 16:
  409. *(u16 *) value = readw(virt_addr);
  410. break;
  411. case 32:
  412. *(u32 *) value = readl(virt_addr);
  413. break;
  414. default:
  415. BUG();
  416. }
  417. iounmap(virt_addr);
  418. return AE_OK;
  419. }
  420. acpi_status
  421. acpi_os_write_memory(acpi_physical_address phys_addr, u32 value, u32 width)
  422. {
  423. void __iomem *virt_addr;
  424. virt_addr = ioremap(phys_addr, width);
  425. switch (width) {
  426. case 8:
  427. writeb(value, virt_addr);
  428. break;
  429. case 16:
  430. writew(value, virt_addr);
  431. break;
  432. case 32:
  433. writel(value, virt_addr);
  434. break;
  435. default:
  436. BUG();
  437. }
  438. iounmap(virt_addr);
  439. return AE_OK;
  440. }
  441. acpi_status
  442. acpi_os_read_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
  443. void *value, u32 width)
  444. {
  445. int result, size;
  446. if (!value)
  447. return AE_BAD_PARAMETER;
  448. switch (width) {
  449. case 8:
  450. size = 1;
  451. break;
  452. case 16:
  453. size = 2;
  454. break;
  455. case 32:
  456. size = 4;
  457. break;
  458. default:
  459. return AE_ERROR;
  460. }
  461. BUG_ON(!raw_pci_ops);
  462. result = raw_pci_ops->read(pci_id->segment, pci_id->bus,
  463. PCI_DEVFN(pci_id->device, pci_id->function),
  464. reg, size, value);
  465. return (result ? AE_ERROR : AE_OK);
  466. }
  467. EXPORT_SYMBOL(acpi_os_read_pci_configuration);
  468. acpi_status
  469. acpi_os_write_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
  470. acpi_integer value, u32 width)
  471. {
  472. int result, size;
  473. switch (width) {
  474. case 8:
  475. size = 1;
  476. break;
  477. case 16:
  478. size = 2;
  479. break;
  480. case 32:
  481. size = 4;
  482. break;
  483. default:
  484. return AE_ERROR;
  485. }
  486. BUG_ON(!raw_pci_ops);
  487. result = raw_pci_ops->write(pci_id->segment, pci_id->bus,
  488. PCI_DEVFN(pci_id->device, pci_id->function),
  489. reg, size, value);
  490. return (result ? AE_ERROR : AE_OK);
  491. }
  492. /* TODO: Change code to take advantage of driver model more */
  493. static void acpi_os_derive_pci_id_2(acpi_handle rhandle, /* upper bound */
  494. acpi_handle chandle, /* current node */
  495. struct acpi_pci_id **id,
  496. int *is_bridge, u8 * bus_number)
  497. {
  498. acpi_handle handle;
  499. struct acpi_pci_id *pci_id = *id;
  500. acpi_status status;
  501. unsigned long temp;
  502. acpi_object_type type;
  503. u8 tu8;
  504. acpi_get_parent(chandle, &handle);
  505. if (handle != rhandle) {
  506. acpi_os_derive_pci_id_2(rhandle, handle, &pci_id, is_bridge,
  507. bus_number);
  508. status = acpi_get_type(handle, &type);
  509. if ((ACPI_FAILURE(status)) || (type != ACPI_TYPE_DEVICE))
  510. return;
  511. status =
  512. acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL,
  513. &temp);
  514. if (ACPI_SUCCESS(status)) {
  515. pci_id->device = ACPI_HIWORD(ACPI_LODWORD(temp));
  516. pci_id->function = ACPI_LOWORD(ACPI_LODWORD(temp));
  517. if (*is_bridge)
  518. pci_id->bus = *bus_number;
  519. /* any nicer way to get bus number of bridge ? */
  520. status =
  521. acpi_os_read_pci_configuration(pci_id, 0x0e, &tu8,
  522. 8);
  523. if (ACPI_SUCCESS(status)
  524. && ((tu8 & 0x7f) == 1 || (tu8 & 0x7f) == 2)) {
  525. status =
  526. acpi_os_read_pci_configuration(pci_id, 0x18,
  527. &tu8, 8);
  528. if (!ACPI_SUCCESS(status)) {
  529. /* Certainly broken... FIX ME */
  530. return;
  531. }
  532. *is_bridge = 1;
  533. pci_id->bus = tu8;
  534. status =
  535. acpi_os_read_pci_configuration(pci_id, 0x19,
  536. &tu8, 8);
  537. if (ACPI_SUCCESS(status)) {
  538. *bus_number = tu8;
  539. }
  540. } else
  541. *is_bridge = 0;
  542. }
  543. }
  544. }
  545. void acpi_os_derive_pci_id(acpi_handle rhandle, /* upper bound */
  546. acpi_handle chandle, /* current node */
  547. struct acpi_pci_id **id)
  548. {
  549. int is_bridge = 1;
  550. u8 bus_number = (*id)->bus;
  551. acpi_os_derive_pci_id_2(rhandle, chandle, id, &is_bridge, &bus_number);
  552. }
  553. static void acpi_os_execute_deferred(struct work_struct *work)
  554. {
  555. struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
  556. if (!dpc) {
  557. printk(KERN_ERR PREFIX "Invalid (NULL) context\n");
  558. return;
  559. }
  560. dpc->function(dpc->context);
  561. kfree(dpc);
  562. /* Yield cpu to notify thread */
  563. cond_resched();
  564. return;
  565. }
  566. static void acpi_os_execute_notify(struct work_struct *work)
  567. {
  568. struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
  569. if (!dpc) {
  570. printk(KERN_ERR PREFIX "Invalid (NULL) context\n");
  571. return;
  572. }
  573. dpc->function(dpc->context);
  574. kfree(dpc);
  575. return;
  576. }
  577. /*******************************************************************************
  578. *
  579. * FUNCTION: acpi_os_execute
  580. *
  581. * PARAMETERS: Type - Type of the callback
  582. * Function - Function to be executed
  583. * Context - Function parameters
  584. *
  585. * RETURN: Status
  586. *
  587. * DESCRIPTION: Depending on type, either queues function for deferred execution or
  588. * immediately executes function on a separate thread.
  589. *
  590. ******************************************************************************/
  591. acpi_status acpi_os_execute(acpi_execute_type type,
  592. acpi_osd_exec_callback function, void *context)
  593. {
  594. acpi_status status = AE_OK;
  595. struct acpi_os_dpc *dpc;
  596. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  597. "Scheduling function [%p(%p)] for deferred execution.\n",
  598. function, context));
  599. if (!function)
  600. return AE_BAD_PARAMETER;
  601. /*
  602. * Allocate/initialize DPC structure. Note that this memory will be
  603. * freed by the callee. The kernel handles the work_struct list in a
  604. * way that allows us to also free its memory inside the callee.
  605. * Because we may want to schedule several tasks with different
  606. * parameters we can't use the approach some kernel code uses of
  607. * having a static work_struct.
  608. */
  609. dpc = kmalloc(sizeof(struct acpi_os_dpc), GFP_ATOMIC);
  610. if (!dpc)
  611. return_ACPI_STATUS(AE_NO_MEMORY);
  612. dpc->function = function;
  613. dpc->context = context;
  614. if (type == OSL_NOTIFY_HANDLER) {
  615. INIT_WORK(&dpc->work, acpi_os_execute_notify);
  616. if (!queue_work(kacpi_notify_wq, &dpc->work)) {
  617. status = AE_ERROR;
  618. kfree(dpc);
  619. }
  620. } else {
  621. INIT_WORK(&dpc->work, acpi_os_execute_deferred);
  622. if (!queue_work(kacpid_wq, &dpc->work)) {
  623. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  624. "Call to queue_work() failed.\n"));
  625. status = AE_ERROR;
  626. kfree(dpc);
  627. }
  628. }
  629. return_ACPI_STATUS(status);
  630. }
  631. EXPORT_SYMBOL(acpi_os_execute);
  632. void acpi_os_wait_events_complete(void *context)
  633. {
  634. flush_workqueue(kacpid_wq);
  635. }
  636. EXPORT_SYMBOL(acpi_os_wait_events_complete);
  637. /*
  638. * Allocate the memory for a spinlock and initialize it.
  639. */
  640. acpi_status acpi_os_create_lock(acpi_spinlock * handle)
  641. {
  642. spin_lock_init(*handle);
  643. return AE_OK;
  644. }
  645. /*
  646. * Deallocate the memory for a spinlock.
  647. */
  648. void acpi_os_delete_lock(acpi_spinlock handle)
  649. {
  650. return;
  651. }
  652. acpi_status
  653. acpi_os_create_semaphore(u32 max_units, u32 initial_units, acpi_handle * handle)
  654. {
  655. struct semaphore *sem = NULL;
  656. sem = acpi_os_allocate(sizeof(struct semaphore));
  657. if (!sem)
  658. return AE_NO_MEMORY;
  659. memset(sem, 0, sizeof(struct semaphore));
  660. sema_init(sem, initial_units);
  661. *handle = (acpi_handle *) sem;
  662. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n",
  663. *handle, initial_units));
  664. return AE_OK;
  665. }
  666. EXPORT_SYMBOL(acpi_os_create_semaphore);
  667. /*
  668. * TODO: A better way to delete semaphores? Linux doesn't have a
  669. * 'delete_semaphore()' function -- may result in an invalid
  670. * pointer dereference for non-synchronized consumers. Should
  671. * we at least check for blocked threads and signal/cancel them?
  672. */
  673. acpi_status acpi_os_delete_semaphore(acpi_handle handle)
  674. {
  675. struct semaphore *sem = (struct semaphore *)handle;
  676. if (!sem)
  677. return AE_BAD_PARAMETER;
  678. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
  679. kfree(sem);
  680. sem = NULL;
  681. return AE_OK;
  682. }
  683. EXPORT_SYMBOL(acpi_os_delete_semaphore);
  684. /*
  685. * TODO: The kernel doesn't have a 'down_timeout' function -- had to
  686. * improvise. The process is to sleep for one scheduler quantum
  687. * until the semaphore becomes available. Downside is that this
  688. * may result in starvation for timeout-based waits when there's
  689. * lots of semaphore activity.
  690. *
  691. * TODO: Support for units > 1?
  692. */
  693. acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
  694. {
  695. acpi_status status = AE_OK;
  696. struct semaphore *sem = (struct semaphore *)handle;
  697. int ret = 0;
  698. if (!sem || (units < 1))
  699. return AE_BAD_PARAMETER;
  700. if (units > 1)
  701. return AE_SUPPORT;
  702. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n",
  703. handle, units, timeout));
  704. /*
  705. * This can be called during resume with interrupts off.
  706. * Like boot-time, we should be single threaded and will
  707. * always get the lock if we try -- timeout or not.
  708. * If this doesn't succeed, then we will oops courtesy of
  709. * might_sleep() in down().
  710. */
  711. if (!down_trylock(sem))
  712. return AE_OK;
  713. switch (timeout) {
  714. /*
  715. * No Wait:
  716. * --------
  717. * A zero timeout value indicates that we shouldn't wait - just
  718. * acquire the semaphore if available otherwise return AE_TIME
  719. * (a.k.a. 'would block').
  720. */
  721. case 0:
  722. if (down_trylock(sem))
  723. status = AE_TIME;
  724. break;
  725. /*
  726. * Wait Indefinitely:
  727. * ------------------
  728. */
  729. case ACPI_WAIT_FOREVER:
  730. down(sem);
  731. break;
  732. /*
  733. * Wait w/ Timeout:
  734. * ----------------
  735. */
  736. default:
  737. // TODO: A better timeout algorithm?
  738. {
  739. int i = 0;
  740. static const int quantum_ms = 1000 / HZ;
  741. ret = down_trylock(sem);
  742. for (i = timeout; (i > 0 && ret != 0); i -= quantum_ms) {
  743. schedule_timeout_interruptible(1);
  744. ret = down_trylock(sem);
  745. }
  746. if (ret != 0)
  747. status = AE_TIME;
  748. }
  749. break;
  750. }
  751. if (ACPI_FAILURE(status)) {
  752. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
  753. "Failed to acquire semaphore[%p|%d|%d], %s",
  754. handle, units, timeout,
  755. acpi_format_exception(status)));
  756. } else {
  757. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
  758. "Acquired semaphore[%p|%d|%d]", handle,
  759. units, timeout));
  760. }
  761. return status;
  762. }
  763. EXPORT_SYMBOL(acpi_os_wait_semaphore);
  764. /*
  765. * TODO: Support for units > 1?
  766. */
  767. acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
  768. {
  769. struct semaphore *sem = (struct semaphore *)handle;
  770. if (!sem || (units < 1))
  771. return AE_BAD_PARAMETER;
  772. if (units > 1)
  773. return AE_SUPPORT;
  774. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle,
  775. units));
  776. up(sem);
  777. return AE_OK;
  778. }
  779. EXPORT_SYMBOL(acpi_os_signal_semaphore);
  780. #ifdef ACPI_FUTURE_USAGE
  781. u32 acpi_os_get_line(char *buffer)
  782. {
  783. #ifdef ENABLE_DEBUGGER
  784. if (acpi_in_debugger) {
  785. u32 chars;
  786. kdb_read(buffer, sizeof(line_buf));
  787. /* remove the CR kdb includes */
  788. chars = strlen(buffer) - 1;
  789. buffer[chars] = '\0';
  790. }
  791. #endif
  792. return 0;
  793. }
  794. #endif /* ACPI_FUTURE_USAGE */
  795. acpi_status acpi_os_signal(u32 function, void *info)
  796. {
  797. switch (function) {
  798. case ACPI_SIGNAL_FATAL:
  799. printk(KERN_ERR PREFIX "Fatal opcode executed\n");
  800. break;
  801. case ACPI_SIGNAL_BREAKPOINT:
  802. /*
  803. * AML Breakpoint
  804. * ACPI spec. says to treat it as a NOP unless
  805. * you are debugging. So if/when we integrate
  806. * AML debugger into the kernel debugger its
  807. * hook will go here. But until then it is
  808. * not useful to print anything on breakpoints.
  809. */
  810. break;
  811. default:
  812. break;
  813. }
  814. return AE_OK;
  815. }
  816. EXPORT_SYMBOL(acpi_os_signal);
  817. static int __init acpi_os_name_setup(char *str)
  818. {
  819. char *p = acpi_os_name;
  820. int count = ACPI_MAX_OVERRIDE_LEN - 1;
  821. if (!str || !*str)
  822. return 0;
  823. for (; count-- && str && *str; str++) {
  824. if (isalnum(*str) || *str == ' ' || *str == ':')
  825. *p++ = *str;
  826. else if (*str == '\'' || *str == '"')
  827. continue;
  828. else
  829. break;
  830. }
  831. *p = 0;
  832. return 1;
  833. }
  834. __setup("acpi_os_name=", acpi_os_name_setup);
  835. static void __init set_osi_linux(unsigned int enable)
  836. {
  837. if (osi_linux.enable != enable) {
  838. osi_linux.enable = enable;
  839. printk(KERN_NOTICE PREFIX "%sed _OSI(Linux)\n",
  840. enable ? "Add": "Delet");
  841. }
  842. return;
  843. }
  844. static void __init acpi_cmdline_osi_linux(unsigned int enable)
  845. {
  846. osi_linux.cmdline = 1; /* cmdline set the default */
  847. set_osi_linux(enable);
  848. return;
  849. }
  850. void __init acpi_dmi_osi_linux(int enable, const struct dmi_system_id *d)
  851. {
  852. osi_linux.dmi = 1; /* DMI knows that this box asks OSI(Linux) */
  853. printk(KERN_NOTICE PREFIX "DMI detected: %s\n", d->ident);
  854. if (enable == -1)
  855. return;
  856. osi_linux.known = 1; /* DMI knows which OSI(Linux) default needed */
  857. set_osi_linux(enable);
  858. return;
  859. }
  860. /*
  861. * Modify the list of "OS Interfaces" reported to BIOS via _OSI
  862. *
  863. * empty string disables _OSI
  864. * string starting with '!' disables that string
  865. * otherwise string is added to list, augmenting built-in strings
  866. */
  867. static int __init acpi_osi_setup(char *str)
  868. {
  869. if (str == NULL || *str == '\0') {
  870. printk(KERN_INFO PREFIX "_OSI method disabled\n");
  871. acpi_gbl_create_osi_method = FALSE;
  872. } else if (!strcmp("!Linux", str)) {
  873. acpi_cmdline_osi_linux(0); /* !enable */
  874. } else if (*str == '!') {
  875. if (acpi_osi_invalidate(++str) == AE_OK)
  876. printk(KERN_INFO PREFIX "Deleted _OSI(%s)\n", str);
  877. } else if (!strcmp("Linux", str)) {
  878. acpi_cmdline_osi_linux(1); /* enable */
  879. } else if (*osi_additional_string == '\0') {
  880. strncpy(osi_additional_string, str, OSI_STRING_LENGTH_MAX);
  881. printk(KERN_INFO PREFIX "Added _OSI(%s)\n", str);
  882. }
  883. return 1;
  884. }
  885. __setup("acpi_osi=", acpi_osi_setup);
  886. /* enable serialization to combat AE_ALREADY_EXISTS errors */
  887. static int __init acpi_serialize_setup(char *str)
  888. {
  889. printk(KERN_INFO PREFIX "serialize enabled\n");
  890. acpi_gbl_all_methods_serialized = TRUE;
  891. return 1;
  892. }
  893. __setup("acpi_serialize", acpi_serialize_setup);
  894. /*
  895. * Wake and Run-Time GPES are expected to be separate.
  896. * We disable wake-GPEs at run-time to prevent spurious
  897. * interrupts.
  898. *
  899. * However, if a system exists that shares Wake and
  900. * Run-time events on the same GPE this flag is available
  901. * to tell Linux to keep the wake-time GPEs enabled at run-time.
  902. */
  903. static int __init acpi_wake_gpes_always_on_setup(char *str)
  904. {
  905. printk(KERN_INFO PREFIX "wake GPEs not disabled\n");
  906. acpi_gbl_leave_wake_gpes_disabled = FALSE;
  907. return 1;
  908. }
  909. __setup("acpi_wake_gpes_always_on", acpi_wake_gpes_always_on_setup);
  910. /*
  911. * Acquire a spinlock.
  912. *
  913. * handle is a pointer to the spinlock_t.
  914. */
  915. acpi_cpu_flags acpi_os_acquire_lock(acpi_spinlock lockp)
  916. {
  917. acpi_cpu_flags flags;
  918. spin_lock_irqsave(lockp, flags);
  919. return flags;
  920. }
  921. /*
  922. * Release a spinlock. See above.
  923. */
  924. void acpi_os_release_lock(acpi_spinlock lockp, acpi_cpu_flags flags)
  925. {
  926. spin_unlock_irqrestore(lockp, flags);
  927. }
  928. #ifndef ACPI_USE_LOCAL_CACHE
  929. /*******************************************************************************
  930. *
  931. * FUNCTION: acpi_os_create_cache
  932. *
  933. * PARAMETERS: name - Ascii name for the cache
  934. * size - Size of each cached object
  935. * depth - Maximum depth of the cache (in objects) <ignored>
  936. * cache - Where the new cache object is returned
  937. *
  938. * RETURN: status
  939. *
  940. * DESCRIPTION: Create a cache object
  941. *
  942. ******************************************************************************/
  943. acpi_status
  944. acpi_os_create_cache(char *name, u16 size, u16 depth, acpi_cache_t ** cache)
  945. {
  946. *cache = kmem_cache_create(name, size, 0, 0, NULL);
  947. if (*cache == NULL)
  948. return AE_ERROR;
  949. else
  950. return AE_OK;
  951. }
  952. /*******************************************************************************
  953. *
  954. * FUNCTION: acpi_os_purge_cache
  955. *
  956. * PARAMETERS: Cache - Handle to cache object
  957. *
  958. * RETURN: Status
  959. *
  960. * DESCRIPTION: Free all objects within the requested cache.
  961. *
  962. ******************************************************************************/
  963. acpi_status acpi_os_purge_cache(acpi_cache_t * cache)
  964. {
  965. kmem_cache_shrink(cache);
  966. return (AE_OK);
  967. }
  968. /*******************************************************************************
  969. *
  970. * FUNCTION: acpi_os_delete_cache
  971. *
  972. * PARAMETERS: Cache - Handle to cache object
  973. *
  974. * RETURN: Status
  975. *
  976. * DESCRIPTION: Free all objects within the requested cache and delete the
  977. * cache object.
  978. *
  979. ******************************************************************************/
  980. acpi_status acpi_os_delete_cache(acpi_cache_t * cache)
  981. {
  982. kmem_cache_destroy(cache);
  983. return (AE_OK);
  984. }
  985. /*******************************************************************************
  986. *
  987. * FUNCTION: acpi_os_release_object
  988. *
  989. * PARAMETERS: Cache - Handle to cache object
  990. * Object - The object to be released
  991. *
  992. * RETURN: None
  993. *
  994. * DESCRIPTION: Release an object to the specified cache. If cache is full,
  995. * the object is deleted.
  996. *
  997. ******************************************************************************/
  998. acpi_status acpi_os_release_object(acpi_cache_t * cache, void *object)
  999. {
  1000. kmem_cache_free(cache, object);
  1001. return (AE_OK);
  1002. }
  1003. /**
  1004. * acpi_dmi_dump - dump DMI slots needed for blacklist entry
  1005. *
  1006. * Returns 0 on success
  1007. */
  1008. static int acpi_dmi_dump(void)
  1009. {
  1010. if (!dmi_available)
  1011. return -1;
  1012. printk(KERN_NOTICE PREFIX "DMI System Vendor: %s\n",
  1013. dmi_get_system_info(DMI_SYS_VENDOR));
  1014. printk(KERN_NOTICE PREFIX "DMI Product Name: %s\n",
  1015. dmi_get_system_info(DMI_PRODUCT_NAME));
  1016. printk(KERN_NOTICE PREFIX "DMI Product Version: %s\n",
  1017. dmi_get_system_info(DMI_PRODUCT_VERSION));
  1018. printk(KERN_NOTICE PREFIX "DMI Board Name: %s\n",
  1019. dmi_get_system_info(DMI_BOARD_NAME));
  1020. printk(KERN_NOTICE PREFIX "DMI BIOS Vendor: %s\n",
  1021. dmi_get_system_info(DMI_BIOS_VENDOR));
  1022. printk(KERN_NOTICE PREFIX "DMI BIOS Date: %s\n",
  1023. dmi_get_system_info(DMI_BIOS_DATE));
  1024. return 0;
  1025. }
  1026. /******************************************************************************
  1027. *
  1028. * FUNCTION: acpi_os_validate_interface
  1029. *
  1030. * PARAMETERS: interface - Requested interface to be validated
  1031. *
  1032. * RETURN: AE_OK if interface is supported, AE_SUPPORT otherwise
  1033. *
  1034. * DESCRIPTION: Match an interface string to the interfaces supported by the
  1035. * host. Strings originate from an AML call to the _OSI method.
  1036. *
  1037. *****************************************************************************/
  1038. acpi_status
  1039. acpi_os_validate_interface (char *interface)
  1040. {
  1041. if (!strncmp(osi_additional_string, interface, OSI_STRING_LENGTH_MAX))
  1042. return AE_OK;
  1043. if (!strcmp("Linux", interface)) {
  1044. printk(KERN_NOTICE PREFIX
  1045. "BIOS _OSI(Linux) query %s%s\n",
  1046. osi_linux.enable ? "honored" : "ignored",
  1047. osi_linux.cmdline ? " via cmdline" :
  1048. osi_linux.dmi ? " via DMI" : "");
  1049. if (!osi_linux.dmi) {
  1050. if (acpi_dmi_dump())
  1051. printk(KERN_NOTICE PREFIX
  1052. "[please extract dmidecode output]\n");
  1053. printk(KERN_NOTICE PREFIX
  1054. "Please send DMI info above to "
  1055. "linux-acpi@vger.kernel.org\n");
  1056. }
  1057. if (!osi_linux.known && !osi_linux.cmdline) {
  1058. printk(KERN_NOTICE PREFIX
  1059. "If \"acpi_osi=%sLinux\" works better, "
  1060. "please notify linux-acpi@vger.kernel.org\n",
  1061. osi_linux.enable ? "!" : "");
  1062. }
  1063. if (osi_linux.enable)
  1064. return AE_OK;
  1065. }
  1066. return AE_SUPPORT;
  1067. }
  1068. /******************************************************************************
  1069. *
  1070. * FUNCTION: acpi_os_validate_address
  1071. *
  1072. * PARAMETERS: space_id - ACPI space ID
  1073. * address - Physical address
  1074. * length - Address length
  1075. *
  1076. * RETURN: AE_OK if address/length is valid for the space_id. Otherwise,
  1077. * should return AE_AML_ILLEGAL_ADDRESS.
  1078. *
  1079. * DESCRIPTION: Validate a system address via the host OS. Used to validate
  1080. * the addresses accessed by AML operation regions.
  1081. *
  1082. *****************************************************************************/
  1083. acpi_status
  1084. acpi_os_validate_address (
  1085. u8 space_id,
  1086. acpi_physical_address address,
  1087. acpi_size length)
  1088. {
  1089. return AE_OK;
  1090. }
  1091. #endif