osl.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290
  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. return;
  563. }
  564. /*******************************************************************************
  565. *
  566. * FUNCTION: acpi_os_execute
  567. *
  568. * PARAMETERS: Type - Type of the callback
  569. * Function - Function to be executed
  570. * Context - Function parameters
  571. *
  572. * RETURN: Status
  573. *
  574. * DESCRIPTION: Depending on type, either queues function for deferred execution or
  575. * immediately executes function on a separate thread.
  576. *
  577. ******************************************************************************/
  578. acpi_status acpi_os_execute(acpi_execute_type type,
  579. acpi_osd_exec_callback function, void *context)
  580. {
  581. acpi_status status = AE_OK;
  582. struct acpi_os_dpc *dpc;
  583. struct workqueue_struct *queue;
  584. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  585. "Scheduling function [%p(%p)] for deferred execution.\n",
  586. function, context));
  587. if (!function)
  588. return AE_BAD_PARAMETER;
  589. /*
  590. * Allocate/initialize DPC structure. Note that this memory will be
  591. * freed by the callee. The kernel handles the work_struct list in a
  592. * way that allows us to also free its memory inside the callee.
  593. * Because we may want to schedule several tasks with different
  594. * parameters we can't use the approach some kernel code uses of
  595. * having a static work_struct.
  596. */
  597. dpc = kmalloc(sizeof(struct acpi_os_dpc), GFP_ATOMIC);
  598. if (!dpc)
  599. return_ACPI_STATUS(AE_NO_MEMORY);
  600. dpc->function = function;
  601. dpc->context = context;
  602. INIT_WORK(&dpc->work, acpi_os_execute_deferred);
  603. queue = (type == OSL_NOTIFY_HANDLER) ? kacpi_notify_wq : kacpid_wq;
  604. if (!queue_work(queue, &dpc->work)) {
  605. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  606. "Call to queue_work() failed.\n"));
  607. status = AE_ERROR;
  608. kfree(dpc);
  609. }
  610. return_ACPI_STATUS(status);
  611. }
  612. EXPORT_SYMBOL(acpi_os_execute);
  613. void acpi_os_wait_events_complete(void *context)
  614. {
  615. flush_workqueue(kacpid_wq);
  616. }
  617. EXPORT_SYMBOL(acpi_os_wait_events_complete);
  618. /*
  619. * Allocate the memory for a spinlock and initialize it.
  620. */
  621. acpi_status acpi_os_create_lock(acpi_spinlock * handle)
  622. {
  623. spin_lock_init(*handle);
  624. return AE_OK;
  625. }
  626. /*
  627. * Deallocate the memory for a spinlock.
  628. */
  629. void acpi_os_delete_lock(acpi_spinlock handle)
  630. {
  631. return;
  632. }
  633. acpi_status
  634. acpi_os_create_semaphore(u32 max_units, u32 initial_units, acpi_handle * handle)
  635. {
  636. struct semaphore *sem = NULL;
  637. sem = acpi_os_allocate(sizeof(struct semaphore));
  638. if (!sem)
  639. return AE_NO_MEMORY;
  640. memset(sem, 0, sizeof(struct semaphore));
  641. sema_init(sem, initial_units);
  642. *handle = (acpi_handle *) sem;
  643. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n",
  644. *handle, initial_units));
  645. return AE_OK;
  646. }
  647. EXPORT_SYMBOL(acpi_os_create_semaphore);
  648. /*
  649. * TODO: A better way to delete semaphores? Linux doesn't have a
  650. * 'delete_semaphore()' function -- may result in an invalid
  651. * pointer dereference for non-synchronized consumers. Should
  652. * we at least check for blocked threads and signal/cancel them?
  653. */
  654. acpi_status acpi_os_delete_semaphore(acpi_handle handle)
  655. {
  656. struct semaphore *sem = (struct semaphore *)handle;
  657. if (!sem)
  658. return AE_BAD_PARAMETER;
  659. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
  660. kfree(sem);
  661. sem = NULL;
  662. return AE_OK;
  663. }
  664. EXPORT_SYMBOL(acpi_os_delete_semaphore);
  665. /*
  666. * TODO: The kernel doesn't have a 'down_timeout' function -- had to
  667. * improvise. The process is to sleep for one scheduler quantum
  668. * until the semaphore becomes available. Downside is that this
  669. * may result in starvation for timeout-based waits when there's
  670. * lots of semaphore activity.
  671. *
  672. * TODO: Support for units > 1?
  673. */
  674. acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
  675. {
  676. acpi_status status = AE_OK;
  677. struct semaphore *sem = (struct semaphore *)handle;
  678. int ret = 0;
  679. if (!sem || (units < 1))
  680. return AE_BAD_PARAMETER;
  681. if (units > 1)
  682. return AE_SUPPORT;
  683. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n",
  684. handle, units, timeout));
  685. /*
  686. * This can be called during resume with interrupts off.
  687. * Like boot-time, we should be single threaded and will
  688. * always get the lock if we try -- timeout or not.
  689. * If this doesn't succeed, then we will oops courtesy of
  690. * might_sleep() in down().
  691. */
  692. if (!down_trylock(sem))
  693. return AE_OK;
  694. switch (timeout) {
  695. /*
  696. * No Wait:
  697. * --------
  698. * A zero timeout value indicates that we shouldn't wait - just
  699. * acquire the semaphore if available otherwise return AE_TIME
  700. * (a.k.a. 'would block').
  701. */
  702. case 0:
  703. if (down_trylock(sem))
  704. status = AE_TIME;
  705. break;
  706. /*
  707. * Wait Indefinitely:
  708. * ------------------
  709. */
  710. case ACPI_WAIT_FOREVER:
  711. down(sem);
  712. break;
  713. /*
  714. * Wait w/ Timeout:
  715. * ----------------
  716. */
  717. default:
  718. // TODO: A better timeout algorithm?
  719. {
  720. int i = 0;
  721. static const int quantum_ms = 1000 / HZ;
  722. ret = down_trylock(sem);
  723. for (i = timeout; (i > 0 && ret != 0); i -= quantum_ms) {
  724. schedule_timeout_interruptible(1);
  725. ret = down_trylock(sem);
  726. }
  727. if (ret != 0)
  728. status = AE_TIME;
  729. }
  730. break;
  731. }
  732. if (ACPI_FAILURE(status)) {
  733. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
  734. "Failed to acquire semaphore[%p|%d|%d], %s",
  735. handle, units, timeout,
  736. acpi_format_exception(status)));
  737. } else {
  738. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
  739. "Acquired semaphore[%p|%d|%d]", handle,
  740. units, timeout));
  741. }
  742. return status;
  743. }
  744. EXPORT_SYMBOL(acpi_os_wait_semaphore);
  745. /*
  746. * TODO: Support for units > 1?
  747. */
  748. acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
  749. {
  750. struct semaphore *sem = (struct semaphore *)handle;
  751. if (!sem || (units < 1))
  752. return AE_BAD_PARAMETER;
  753. if (units > 1)
  754. return AE_SUPPORT;
  755. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle,
  756. units));
  757. up(sem);
  758. return AE_OK;
  759. }
  760. EXPORT_SYMBOL(acpi_os_signal_semaphore);
  761. #ifdef ACPI_FUTURE_USAGE
  762. u32 acpi_os_get_line(char *buffer)
  763. {
  764. #ifdef ENABLE_DEBUGGER
  765. if (acpi_in_debugger) {
  766. u32 chars;
  767. kdb_read(buffer, sizeof(line_buf));
  768. /* remove the CR kdb includes */
  769. chars = strlen(buffer) - 1;
  770. buffer[chars] = '\0';
  771. }
  772. #endif
  773. return 0;
  774. }
  775. #endif /* ACPI_FUTURE_USAGE */
  776. acpi_status acpi_os_signal(u32 function, void *info)
  777. {
  778. switch (function) {
  779. case ACPI_SIGNAL_FATAL:
  780. printk(KERN_ERR PREFIX "Fatal opcode executed\n");
  781. break;
  782. case ACPI_SIGNAL_BREAKPOINT:
  783. /*
  784. * AML Breakpoint
  785. * ACPI spec. says to treat it as a NOP unless
  786. * you are debugging. So if/when we integrate
  787. * AML debugger into the kernel debugger its
  788. * hook will go here. But until then it is
  789. * not useful to print anything on breakpoints.
  790. */
  791. break;
  792. default:
  793. break;
  794. }
  795. return AE_OK;
  796. }
  797. EXPORT_SYMBOL(acpi_os_signal);
  798. static int __init acpi_os_name_setup(char *str)
  799. {
  800. char *p = acpi_os_name;
  801. int count = ACPI_MAX_OVERRIDE_LEN - 1;
  802. if (!str || !*str)
  803. return 0;
  804. for (; count-- && str && *str; str++) {
  805. if (isalnum(*str) || *str == ' ' || *str == ':')
  806. *p++ = *str;
  807. else if (*str == '\'' || *str == '"')
  808. continue;
  809. else
  810. break;
  811. }
  812. *p = 0;
  813. return 1;
  814. }
  815. __setup("acpi_os_name=", acpi_os_name_setup);
  816. static void __init set_osi_linux(unsigned int enable)
  817. {
  818. if (osi_linux.enable != enable) {
  819. osi_linux.enable = enable;
  820. printk(KERN_NOTICE PREFIX "%sed _OSI(Linux)\n",
  821. enable ? "Add": "Delet");
  822. }
  823. return;
  824. }
  825. static void __init acpi_cmdline_osi_linux(unsigned int enable)
  826. {
  827. osi_linux.cmdline = 1; /* cmdline set the default */
  828. set_osi_linux(enable);
  829. return;
  830. }
  831. void __init acpi_dmi_osi_linux(int enable, const struct dmi_system_id *d)
  832. {
  833. osi_linux.dmi = 1; /* DMI knows that this box asks OSI(Linux) */
  834. printk(KERN_NOTICE PREFIX "DMI detected: %s\n", d->ident);
  835. if (enable == -1)
  836. return;
  837. osi_linux.known = 1; /* DMI knows which OSI(Linux) default needed */
  838. set_osi_linux(enable);
  839. return;
  840. }
  841. /*
  842. * Modify the list of "OS Interfaces" reported to BIOS via _OSI
  843. *
  844. * empty string disables _OSI
  845. * string starting with '!' disables that string
  846. * otherwise string is added to list, augmenting built-in strings
  847. */
  848. static int __init acpi_osi_setup(char *str)
  849. {
  850. if (str == NULL || *str == '\0') {
  851. printk(KERN_INFO PREFIX "_OSI method disabled\n");
  852. acpi_gbl_create_osi_method = FALSE;
  853. } else if (!strcmp("!Linux", str)) {
  854. acpi_cmdline_osi_linux(0); /* !enable */
  855. } else if (*str == '!') {
  856. if (acpi_osi_invalidate(++str) == AE_OK)
  857. printk(KERN_INFO PREFIX "Deleted _OSI(%s)\n", str);
  858. } else if (!strcmp("Linux", str)) {
  859. acpi_cmdline_osi_linux(1); /* enable */
  860. } else if (*osi_additional_string == '\0') {
  861. strncpy(osi_additional_string, str, OSI_STRING_LENGTH_MAX);
  862. printk(KERN_INFO PREFIX "Added _OSI(%s)\n", str);
  863. }
  864. return 1;
  865. }
  866. __setup("acpi_osi=", acpi_osi_setup);
  867. /* enable serialization to combat AE_ALREADY_EXISTS errors */
  868. static int __init acpi_serialize_setup(char *str)
  869. {
  870. printk(KERN_INFO PREFIX "serialize enabled\n");
  871. acpi_gbl_all_methods_serialized = TRUE;
  872. return 1;
  873. }
  874. __setup("acpi_serialize", acpi_serialize_setup);
  875. /*
  876. * Wake and Run-Time GPES are expected to be separate.
  877. * We disable wake-GPEs at run-time to prevent spurious
  878. * interrupts.
  879. *
  880. * However, if a system exists that shares Wake and
  881. * Run-time events on the same GPE this flag is available
  882. * to tell Linux to keep the wake-time GPEs enabled at run-time.
  883. */
  884. static int __init acpi_wake_gpes_always_on_setup(char *str)
  885. {
  886. printk(KERN_INFO PREFIX "wake GPEs not disabled\n");
  887. acpi_gbl_leave_wake_gpes_disabled = FALSE;
  888. return 1;
  889. }
  890. __setup("acpi_wake_gpes_always_on", acpi_wake_gpes_always_on_setup);
  891. /*
  892. * Acquire a spinlock.
  893. *
  894. * handle is a pointer to the spinlock_t.
  895. */
  896. acpi_cpu_flags acpi_os_acquire_lock(acpi_spinlock lockp)
  897. {
  898. acpi_cpu_flags flags;
  899. spin_lock_irqsave(lockp, flags);
  900. return flags;
  901. }
  902. /*
  903. * Release a spinlock. See above.
  904. */
  905. void acpi_os_release_lock(acpi_spinlock lockp, acpi_cpu_flags flags)
  906. {
  907. spin_unlock_irqrestore(lockp, flags);
  908. }
  909. #ifndef ACPI_USE_LOCAL_CACHE
  910. /*******************************************************************************
  911. *
  912. * FUNCTION: acpi_os_create_cache
  913. *
  914. * PARAMETERS: name - Ascii name for the cache
  915. * size - Size of each cached object
  916. * depth - Maximum depth of the cache (in objects) <ignored>
  917. * cache - Where the new cache object is returned
  918. *
  919. * RETURN: status
  920. *
  921. * DESCRIPTION: Create a cache object
  922. *
  923. ******************************************************************************/
  924. acpi_status
  925. acpi_os_create_cache(char *name, u16 size, u16 depth, acpi_cache_t ** cache)
  926. {
  927. *cache = kmem_cache_create(name, size, 0, 0, NULL);
  928. if (*cache == NULL)
  929. return AE_ERROR;
  930. else
  931. return AE_OK;
  932. }
  933. /*******************************************************************************
  934. *
  935. * FUNCTION: acpi_os_purge_cache
  936. *
  937. * PARAMETERS: Cache - Handle to cache object
  938. *
  939. * RETURN: Status
  940. *
  941. * DESCRIPTION: Free all objects within the requested cache.
  942. *
  943. ******************************************************************************/
  944. acpi_status acpi_os_purge_cache(acpi_cache_t * cache)
  945. {
  946. kmem_cache_shrink(cache);
  947. return (AE_OK);
  948. }
  949. /*******************************************************************************
  950. *
  951. * FUNCTION: acpi_os_delete_cache
  952. *
  953. * PARAMETERS: Cache - Handle to cache object
  954. *
  955. * RETURN: Status
  956. *
  957. * DESCRIPTION: Free all objects within the requested cache and delete the
  958. * cache object.
  959. *
  960. ******************************************************************************/
  961. acpi_status acpi_os_delete_cache(acpi_cache_t * cache)
  962. {
  963. kmem_cache_destroy(cache);
  964. return (AE_OK);
  965. }
  966. /*******************************************************************************
  967. *
  968. * FUNCTION: acpi_os_release_object
  969. *
  970. * PARAMETERS: Cache - Handle to cache object
  971. * Object - The object to be released
  972. *
  973. * RETURN: None
  974. *
  975. * DESCRIPTION: Release an object to the specified cache. If cache is full,
  976. * the object is deleted.
  977. *
  978. ******************************************************************************/
  979. acpi_status acpi_os_release_object(acpi_cache_t * cache, void *object)
  980. {
  981. kmem_cache_free(cache, object);
  982. return (AE_OK);
  983. }
  984. /**
  985. * acpi_dmi_dump - dump DMI slots needed for blacklist entry
  986. *
  987. * Returns 0 on success
  988. */
  989. static int acpi_dmi_dump(void)
  990. {
  991. if (!dmi_available)
  992. return -1;
  993. printk(KERN_NOTICE PREFIX "DMI System Vendor: %s\n",
  994. dmi_get_system_info(DMI_SYS_VENDOR));
  995. printk(KERN_NOTICE PREFIX "DMI Product Name: %s\n",
  996. dmi_get_system_info(DMI_PRODUCT_NAME));
  997. printk(KERN_NOTICE PREFIX "DMI Product Version: %s\n",
  998. dmi_get_system_info(DMI_PRODUCT_VERSION));
  999. printk(KERN_NOTICE PREFIX "DMI Board Name: %s\n",
  1000. dmi_get_system_info(DMI_BOARD_NAME));
  1001. printk(KERN_NOTICE PREFIX "DMI BIOS Vendor: %s\n",
  1002. dmi_get_system_info(DMI_BIOS_VENDOR));
  1003. printk(KERN_NOTICE PREFIX "DMI BIOS Date: %s\n",
  1004. dmi_get_system_info(DMI_BIOS_DATE));
  1005. return 0;
  1006. }
  1007. /******************************************************************************
  1008. *
  1009. * FUNCTION: acpi_os_validate_interface
  1010. *
  1011. * PARAMETERS: interface - Requested interface to be validated
  1012. *
  1013. * RETURN: AE_OK if interface is supported, AE_SUPPORT otherwise
  1014. *
  1015. * DESCRIPTION: Match an interface string to the interfaces supported by the
  1016. * host. Strings originate from an AML call to the _OSI method.
  1017. *
  1018. *****************************************************************************/
  1019. acpi_status
  1020. acpi_os_validate_interface (char *interface)
  1021. {
  1022. if (!strncmp(osi_additional_string, interface, OSI_STRING_LENGTH_MAX))
  1023. return AE_OK;
  1024. if (!strcmp("Linux", interface)) {
  1025. printk(KERN_NOTICE PREFIX
  1026. "BIOS _OSI(Linux) query %s%s\n",
  1027. osi_linux.enable ? "honored" : "ignored",
  1028. osi_linux.cmdline ? " via cmdline" :
  1029. osi_linux.dmi ? " via DMI" : "");
  1030. if (!osi_linux.dmi) {
  1031. if (acpi_dmi_dump())
  1032. printk(KERN_NOTICE PREFIX
  1033. "[please extract dmidecode output]\n");
  1034. printk(KERN_NOTICE PREFIX
  1035. "Please send DMI info above to "
  1036. "linux-acpi@vger.kernel.org\n");
  1037. }
  1038. if (!osi_linux.known && !osi_linux.cmdline) {
  1039. printk(KERN_NOTICE PREFIX
  1040. "If \"acpi_osi=%sLinux\" works better, "
  1041. "please notify linux-acpi@vger.kernel.org\n",
  1042. osi_linux.enable ? "!" : "");
  1043. }
  1044. if (osi_linux.enable)
  1045. return AE_OK;
  1046. }
  1047. return AE_SUPPORT;
  1048. }
  1049. /******************************************************************************
  1050. *
  1051. * FUNCTION: acpi_os_validate_address
  1052. *
  1053. * PARAMETERS: space_id - ACPI space ID
  1054. * address - Physical address
  1055. * length - Address length
  1056. *
  1057. * RETURN: AE_OK if address/length is valid for the space_id. Otherwise,
  1058. * should return AE_AML_ILLEGAL_ADDRESS.
  1059. *
  1060. * DESCRIPTION: Validate a system address via the host OS. Used to validate
  1061. * the addresses accessed by AML operation regions.
  1062. *
  1063. *****************************************************************************/
  1064. acpi_status
  1065. acpi_os_validate_address (
  1066. u8 space_id,
  1067. acpi_physical_address address,
  1068. acpi_size length)
  1069. {
  1070. return AE_OK;
  1071. }
  1072. #endif