osl.c 30 KB

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