osl.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  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/smp_lock.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/kmod.h>
  35. #include <linux/delay.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. int acpi_specific_hotkey_enabled = TRUE;
  64. EXPORT_SYMBOL(acpi_specific_hotkey_enabled);
  65. static unsigned int acpi_irq_irq;
  66. static acpi_osd_handler acpi_irq_handler;
  67. static void *acpi_irq_context;
  68. static struct workqueue_struct *kacpid_wq;
  69. static void __init acpi_request_region (struct acpi_generic_address *addr,
  70. unsigned int length, char *desc)
  71. {
  72. struct resource *res;
  73. if (!addr->address || !length)
  74. return;
  75. if (addr->space_id == ACPI_ADR_SPACE_SYSTEM_IO)
  76. res = request_region(addr->address, length, desc);
  77. else if (addr->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
  78. res = request_mem_region(addr->address, length, desc);
  79. }
  80. static int __init acpi_reserve_resources(void)
  81. {
  82. acpi_request_region(&acpi_gbl_FADT.xpm1a_event_block, acpi_gbl_FADT.pm1_event_length,
  83. "ACPI PM1a_EVT_BLK");
  84. acpi_request_region(&acpi_gbl_FADT.xpm1b_event_block, acpi_gbl_FADT.pm1_event_length,
  85. "ACPI PM1b_EVT_BLK");
  86. acpi_request_region(&acpi_gbl_FADT.xpm1a_control_block, acpi_gbl_FADT.pm1_control_length,
  87. "ACPI PM1a_CNT_BLK");
  88. acpi_request_region(&acpi_gbl_FADT.xpm1b_control_block, acpi_gbl_FADT.pm1_control_length,
  89. "ACPI PM1b_CNT_BLK");
  90. if (acpi_gbl_FADT.pm_timer_length == 4)
  91. acpi_request_region(&acpi_gbl_FADT.xpm_timer_block, 4, "ACPI PM_TMR");
  92. acpi_request_region(&acpi_gbl_FADT.xpm2_control_block, acpi_gbl_FADT.pm2_control_length,
  93. "ACPI PM2_CNT_BLK");
  94. /* Length of GPE blocks must be a non-negative multiple of 2 */
  95. if (!(acpi_gbl_FADT.gpe0_block_length & 0x1))
  96. acpi_request_region(&acpi_gbl_FADT.xgpe0_block,
  97. acpi_gbl_FADT.gpe0_block_length, "ACPI GPE0_BLK");
  98. if (!(acpi_gbl_FADT.gpe1_block_length & 0x1))
  99. acpi_request_region(&acpi_gbl_FADT.xgpe1_block,
  100. acpi_gbl_FADT.gpe1_block_length, "ACPI GPE1_BLK");
  101. return 0;
  102. }
  103. device_initcall(acpi_reserve_resources);
  104. acpi_status acpi_os_initialize(void)
  105. {
  106. return AE_OK;
  107. }
  108. acpi_status acpi_os_initialize1(void)
  109. {
  110. /*
  111. * Initialize PCI configuration space access, as we'll need to access
  112. * it while walking the namespace (bus 0 and root bridges w/ _BBNs).
  113. */
  114. if (!raw_pci_ops) {
  115. printk(KERN_ERR PREFIX
  116. "Access to PCI configuration space unavailable\n");
  117. return AE_NULL_ENTRY;
  118. }
  119. kacpid_wq = create_singlethread_workqueue("kacpid");
  120. BUG_ON(!kacpid_wq);
  121. return AE_OK;
  122. }
  123. acpi_status acpi_os_terminate(void)
  124. {
  125. if (acpi_irq_handler) {
  126. acpi_os_remove_interrupt_handler(acpi_irq_irq,
  127. acpi_irq_handler);
  128. }
  129. destroy_workqueue(kacpid_wq);
  130. return AE_OK;
  131. }
  132. void acpi_os_printf(const char *fmt, ...)
  133. {
  134. va_list args;
  135. va_start(args, fmt);
  136. acpi_os_vprintf(fmt, args);
  137. va_end(args);
  138. }
  139. EXPORT_SYMBOL(acpi_os_printf);
  140. void acpi_os_vprintf(const char *fmt, va_list args)
  141. {
  142. static char buffer[512];
  143. vsprintf(buffer, fmt, args);
  144. #ifdef ENABLE_DEBUGGER
  145. if (acpi_in_debugger) {
  146. kdb_printf("%s", buffer);
  147. } else {
  148. printk("%s", buffer);
  149. }
  150. #else
  151. printk("%s", buffer);
  152. #endif
  153. }
  154. acpi_physical_address __init acpi_os_get_root_pointer(void)
  155. {
  156. if (efi_enabled) {
  157. if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
  158. return efi.acpi20;
  159. else if (efi.acpi != EFI_INVALID_TABLE_ADDR)
  160. return efi.acpi;
  161. else {
  162. printk(KERN_ERR PREFIX
  163. "System description tables not found\n");
  164. return 0;
  165. }
  166. } else
  167. return acpi_find_rsdp();
  168. }
  169. void __iomem *acpi_os_map_memory(acpi_physical_address phys, acpi_size size)
  170. {
  171. if (phys > ULONG_MAX) {
  172. printk(KERN_ERR PREFIX "Cannot map memory that high\n");
  173. return 0;
  174. }
  175. if (acpi_gbl_permanent_mmap)
  176. /*
  177. * ioremap checks to ensure this is in reserved space
  178. */
  179. return ioremap((unsigned long)phys, size);
  180. else
  181. return __acpi_map_table((unsigned long)phys, size);
  182. }
  183. EXPORT_SYMBOL_GPL(acpi_os_map_memory);
  184. void acpi_os_unmap_memory(void __iomem * virt, acpi_size size)
  185. {
  186. if (acpi_gbl_permanent_mmap) {
  187. iounmap(virt);
  188. }
  189. }
  190. EXPORT_SYMBOL_GPL(acpi_os_unmap_memory);
  191. #ifdef ACPI_FUTURE_USAGE
  192. acpi_status
  193. acpi_os_get_physical_address(void *virt, acpi_physical_address * phys)
  194. {
  195. if (!phys || !virt)
  196. return AE_BAD_PARAMETER;
  197. *phys = virt_to_phys(virt);
  198. return AE_OK;
  199. }
  200. #endif
  201. #define ACPI_MAX_OVERRIDE_LEN 100
  202. static char acpi_os_name[ACPI_MAX_OVERRIDE_LEN];
  203. acpi_status
  204. acpi_os_predefined_override(const struct acpi_predefined_names *init_val,
  205. acpi_string * new_val)
  206. {
  207. if (!init_val || !new_val)
  208. return AE_BAD_PARAMETER;
  209. *new_val = NULL;
  210. if (!memcmp(init_val->name, "_OS_", 4) && strlen(acpi_os_name)) {
  211. printk(KERN_INFO PREFIX "Overriding _OS definition to '%s'\n",
  212. acpi_os_name);
  213. *new_val = acpi_os_name;
  214. }
  215. return AE_OK;
  216. }
  217. acpi_status
  218. acpi_os_table_override(struct acpi_table_header * existing_table,
  219. struct acpi_table_header ** new_table)
  220. {
  221. if (!existing_table || !new_table)
  222. return AE_BAD_PARAMETER;
  223. #ifdef CONFIG_ACPI_CUSTOM_DSDT
  224. if (strncmp(existing_table->signature, "DSDT", 4) == 0)
  225. *new_table = (struct acpi_table_header *)AmlCode;
  226. else
  227. *new_table = NULL;
  228. #else
  229. *new_table = NULL;
  230. #endif
  231. return AE_OK;
  232. }
  233. static irqreturn_t acpi_irq(int irq, void *dev_id)
  234. {
  235. return (*acpi_irq_handler) (acpi_irq_context) ? IRQ_HANDLED : IRQ_NONE;
  236. }
  237. acpi_status
  238. acpi_os_install_interrupt_handler(u32 gsi, acpi_osd_handler handler,
  239. void *context)
  240. {
  241. unsigned int irq;
  242. /*
  243. * Ignore the GSI from the core, and use the value in our copy of the
  244. * FADT. It may not be the same if an interrupt source override exists
  245. * for the SCI.
  246. */
  247. gsi = acpi_gbl_FADT.sci_interrupt;
  248. if (acpi_gsi_to_irq(gsi, &irq) < 0) {
  249. printk(KERN_ERR PREFIX "SCI (ACPI GSI %d) not registered\n",
  250. gsi);
  251. return AE_OK;
  252. }
  253. acpi_irq_handler = handler;
  254. acpi_irq_context = context;
  255. if (request_irq(irq, acpi_irq, IRQF_SHARED, "acpi", acpi_irq)) {
  256. printk(KERN_ERR PREFIX "SCI (IRQ%d) allocation failed\n", irq);
  257. return AE_NOT_ACQUIRED;
  258. }
  259. acpi_irq_irq = irq;
  260. return AE_OK;
  261. }
  262. acpi_status acpi_os_remove_interrupt_handler(u32 irq, acpi_osd_handler handler)
  263. {
  264. if (irq) {
  265. free_irq(irq, acpi_irq);
  266. acpi_irq_handler = NULL;
  267. acpi_irq_irq = 0;
  268. }
  269. return AE_OK;
  270. }
  271. /*
  272. * Running in interpreter thread context, safe to sleep
  273. */
  274. void acpi_os_sleep(acpi_integer ms)
  275. {
  276. schedule_timeout_interruptible(msecs_to_jiffies(ms));
  277. }
  278. EXPORT_SYMBOL(acpi_os_sleep);
  279. void acpi_os_stall(u32 us)
  280. {
  281. while (us) {
  282. u32 delay = 1000;
  283. if (delay > us)
  284. delay = us;
  285. udelay(delay);
  286. touch_nmi_watchdog();
  287. us -= delay;
  288. }
  289. }
  290. EXPORT_SYMBOL(acpi_os_stall);
  291. /*
  292. * Support ACPI 3.0 AML Timer operand
  293. * Returns 64-bit free-running, monotonically increasing timer
  294. * with 100ns granularity
  295. */
  296. u64 acpi_os_get_timer(void)
  297. {
  298. static u64 t;
  299. #ifdef CONFIG_HPET
  300. /* TBD: use HPET if available */
  301. #endif
  302. #ifdef CONFIG_X86_PM_TIMER
  303. /* TBD: default to PM timer if HPET was not available */
  304. #endif
  305. if (!t)
  306. printk(KERN_ERR PREFIX "acpi_os_get_timer() TBD\n");
  307. return ++t;
  308. }
  309. acpi_status acpi_os_read_port(acpi_io_address port, u32 * value, u32 width)
  310. {
  311. u32 dummy;
  312. if (!value)
  313. value = &dummy;
  314. switch (width) {
  315. case 8:
  316. *(u8 *) value = inb(port);
  317. break;
  318. case 16:
  319. *(u16 *) value = inw(port);
  320. break;
  321. case 32:
  322. *(u32 *) value = inl(port);
  323. break;
  324. default:
  325. BUG();
  326. }
  327. return AE_OK;
  328. }
  329. EXPORT_SYMBOL(acpi_os_read_port);
  330. acpi_status acpi_os_write_port(acpi_io_address port, u32 value, u32 width)
  331. {
  332. switch (width) {
  333. case 8:
  334. outb(value, port);
  335. break;
  336. case 16:
  337. outw(value, port);
  338. break;
  339. case 32:
  340. outl(value, port);
  341. break;
  342. default:
  343. BUG();
  344. }
  345. return AE_OK;
  346. }
  347. EXPORT_SYMBOL(acpi_os_write_port);
  348. acpi_status
  349. acpi_os_read_memory(acpi_physical_address phys_addr, u32 * value, u32 width)
  350. {
  351. u32 dummy;
  352. void __iomem *virt_addr;
  353. virt_addr = ioremap(phys_addr, width);
  354. if (!value)
  355. value = &dummy;
  356. switch (width) {
  357. case 8:
  358. *(u8 *) value = readb(virt_addr);
  359. break;
  360. case 16:
  361. *(u16 *) value = readw(virt_addr);
  362. break;
  363. case 32:
  364. *(u32 *) value = readl(virt_addr);
  365. break;
  366. default:
  367. BUG();
  368. }
  369. iounmap(virt_addr);
  370. return AE_OK;
  371. }
  372. acpi_status
  373. acpi_os_write_memory(acpi_physical_address phys_addr, u32 value, u32 width)
  374. {
  375. void __iomem *virt_addr;
  376. virt_addr = ioremap(phys_addr, width);
  377. switch (width) {
  378. case 8:
  379. writeb(value, virt_addr);
  380. break;
  381. case 16:
  382. writew(value, virt_addr);
  383. break;
  384. case 32:
  385. writel(value, virt_addr);
  386. break;
  387. default:
  388. BUG();
  389. }
  390. iounmap(virt_addr);
  391. return AE_OK;
  392. }
  393. acpi_status
  394. acpi_os_read_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
  395. void *value, u32 width)
  396. {
  397. int result, size;
  398. if (!value)
  399. return AE_BAD_PARAMETER;
  400. switch (width) {
  401. case 8:
  402. size = 1;
  403. break;
  404. case 16:
  405. size = 2;
  406. break;
  407. case 32:
  408. size = 4;
  409. break;
  410. default:
  411. return AE_ERROR;
  412. }
  413. BUG_ON(!raw_pci_ops);
  414. result = raw_pci_ops->read(pci_id->segment, pci_id->bus,
  415. PCI_DEVFN(pci_id->device, pci_id->function),
  416. reg, size, value);
  417. return (result ? AE_ERROR : AE_OK);
  418. }
  419. EXPORT_SYMBOL(acpi_os_read_pci_configuration);
  420. acpi_status
  421. acpi_os_write_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
  422. acpi_integer value, u32 width)
  423. {
  424. int result, size;
  425. switch (width) {
  426. case 8:
  427. size = 1;
  428. break;
  429. case 16:
  430. size = 2;
  431. break;
  432. case 32:
  433. size = 4;
  434. break;
  435. default:
  436. return AE_ERROR;
  437. }
  438. BUG_ON(!raw_pci_ops);
  439. result = raw_pci_ops->write(pci_id->segment, pci_id->bus,
  440. PCI_DEVFN(pci_id->device, pci_id->function),
  441. reg, size, value);
  442. return (result ? AE_ERROR : AE_OK);
  443. }
  444. /* TODO: Change code to take advantage of driver model more */
  445. static void acpi_os_derive_pci_id_2(acpi_handle rhandle, /* upper bound */
  446. acpi_handle chandle, /* current node */
  447. struct acpi_pci_id **id,
  448. int *is_bridge, u8 * bus_number)
  449. {
  450. acpi_handle handle;
  451. struct acpi_pci_id *pci_id = *id;
  452. acpi_status status;
  453. unsigned long temp;
  454. acpi_object_type type;
  455. u8 tu8;
  456. acpi_get_parent(chandle, &handle);
  457. if (handle != rhandle) {
  458. acpi_os_derive_pci_id_2(rhandle, handle, &pci_id, is_bridge,
  459. bus_number);
  460. status = acpi_get_type(handle, &type);
  461. if ((ACPI_FAILURE(status)) || (type != ACPI_TYPE_DEVICE))
  462. return;
  463. status =
  464. acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL,
  465. &temp);
  466. if (ACPI_SUCCESS(status)) {
  467. pci_id->device = ACPI_HIWORD(ACPI_LODWORD(temp));
  468. pci_id->function = ACPI_LOWORD(ACPI_LODWORD(temp));
  469. if (*is_bridge)
  470. pci_id->bus = *bus_number;
  471. /* any nicer way to get bus number of bridge ? */
  472. status =
  473. acpi_os_read_pci_configuration(pci_id, 0x0e, &tu8,
  474. 8);
  475. if (ACPI_SUCCESS(status)
  476. && ((tu8 & 0x7f) == 1 || (tu8 & 0x7f) == 2)) {
  477. status =
  478. acpi_os_read_pci_configuration(pci_id, 0x18,
  479. &tu8, 8);
  480. if (!ACPI_SUCCESS(status)) {
  481. /* Certainly broken... FIX ME */
  482. return;
  483. }
  484. *is_bridge = 1;
  485. pci_id->bus = tu8;
  486. status =
  487. acpi_os_read_pci_configuration(pci_id, 0x19,
  488. &tu8, 8);
  489. if (ACPI_SUCCESS(status)) {
  490. *bus_number = tu8;
  491. }
  492. } else
  493. *is_bridge = 0;
  494. }
  495. }
  496. }
  497. void acpi_os_derive_pci_id(acpi_handle rhandle, /* upper bound */
  498. acpi_handle chandle, /* current node */
  499. struct acpi_pci_id **id)
  500. {
  501. int is_bridge = 1;
  502. u8 bus_number = (*id)->bus;
  503. acpi_os_derive_pci_id_2(rhandle, chandle, id, &is_bridge, &bus_number);
  504. }
  505. static void acpi_os_execute_deferred(struct work_struct *work)
  506. {
  507. struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
  508. if (!dpc) {
  509. printk(KERN_ERR PREFIX "Invalid (NULL) context\n");
  510. return;
  511. }
  512. dpc->function(dpc->context);
  513. kfree(dpc);
  514. return;
  515. }
  516. /*******************************************************************************
  517. *
  518. * FUNCTION: acpi_os_execute
  519. *
  520. * PARAMETERS: Type - Type of the callback
  521. * Function - Function to be executed
  522. * Context - Function parameters
  523. *
  524. * RETURN: Status
  525. *
  526. * DESCRIPTION: Depending on type, either queues function for deferred execution or
  527. * immediately executes function on a separate thread.
  528. *
  529. ******************************************************************************/
  530. acpi_status acpi_os_execute(acpi_execute_type type,
  531. acpi_osd_exec_callback function, void *context)
  532. {
  533. acpi_status status = AE_OK;
  534. struct acpi_os_dpc *dpc;
  535. ACPI_FUNCTION_TRACE("os_queue_for_execution");
  536. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  537. "Scheduling function [%p(%p)] for deferred execution.\n",
  538. function, context));
  539. if (!function)
  540. return_ACPI_STATUS(AE_BAD_PARAMETER);
  541. /*
  542. * Allocate/initialize DPC structure. Note that this memory will be
  543. * freed by the callee. The kernel handles the work_struct list in a
  544. * way that allows us to also free its memory inside the callee.
  545. * Because we may want to schedule several tasks with different
  546. * parameters we can't use the approach some kernel code uses of
  547. * having a static work_struct.
  548. */
  549. dpc = kmalloc(sizeof(struct acpi_os_dpc), GFP_ATOMIC);
  550. if (!dpc)
  551. return_ACPI_STATUS(AE_NO_MEMORY);
  552. dpc->function = function;
  553. dpc->context = context;
  554. INIT_WORK(&dpc->work, acpi_os_execute_deferred);
  555. if (!queue_work(kacpid_wq, &dpc->work)) {
  556. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  557. "Call to queue_work() failed.\n"));
  558. kfree(dpc);
  559. status = AE_ERROR;
  560. }
  561. return_ACPI_STATUS(status);
  562. }
  563. EXPORT_SYMBOL(acpi_os_execute);
  564. void acpi_os_wait_events_complete(void *context)
  565. {
  566. flush_workqueue(kacpid_wq);
  567. }
  568. EXPORT_SYMBOL(acpi_os_wait_events_complete);
  569. /*
  570. * Allocate the memory for a spinlock and initialize it.
  571. */
  572. acpi_status acpi_os_create_lock(acpi_spinlock * handle)
  573. {
  574. spin_lock_init(*handle);
  575. return AE_OK;
  576. }
  577. /*
  578. * Deallocate the memory for a spinlock.
  579. */
  580. void acpi_os_delete_lock(acpi_spinlock handle)
  581. {
  582. return;
  583. }
  584. acpi_status
  585. acpi_os_create_semaphore(u32 max_units, u32 initial_units, acpi_handle * handle)
  586. {
  587. struct semaphore *sem = NULL;
  588. sem = acpi_os_allocate(sizeof(struct semaphore));
  589. if (!sem)
  590. return AE_NO_MEMORY;
  591. memset(sem, 0, sizeof(struct semaphore));
  592. sema_init(sem, initial_units);
  593. *handle = (acpi_handle *) sem;
  594. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n",
  595. *handle, initial_units));
  596. return AE_OK;
  597. }
  598. EXPORT_SYMBOL(acpi_os_create_semaphore);
  599. /*
  600. * TODO: A better way to delete semaphores? Linux doesn't have a
  601. * 'delete_semaphore()' function -- may result in an invalid
  602. * pointer dereference for non-synchronized consumers. Should
  603. * we at least check for blocked threads and signal/cancel them?
  604. */
  605. acpi_status acpi_os_delete_semaphore(acpi_handle handle)
  606. {
  607. struct semaphore *sem = (struct semaphore *)handle;
  608. if (!sem)
  609. return AE_BAD_PARAMETER;
  610. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
  611. kfree(sem);
  612. sem = NULL;
  613. return AE_OK;
  614. }
  615. EXPORT_SYMBOL(acpi_os_delete_semaphore);
  616. /*
  617. * TODO: The kernel doesn't have a 'down_timeout' function -- had to
  618. * improvise. The process is to sleep for one scheduler quantum
  619. * until the semaphore becomes available. Downside is that this
  620. * may result in starvation for timeout-based waits when there's
  621. * lots of semaphore activity.
  622. *
  623. * TODO: Support for units > 1?
  624. */
  625. acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
  626. {
  627. acpi_status status = AE_OK;
  628. struct semaphore *sem = (struct semaphore *)handle;
  629. int ret = 0;
  630. if (!sem || (units < 1))
  631. return AE_BAD_PARAMETER;
  632. if (units > 1)
  633. return AE_SUPPORT;
  634. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n",
  635. handle, units, timeout));
  636. /*
  637. * This can be called during resume with interrupts off.
  638. * Like boot-time, we should be single threaded and will
  639. * always get the lock if we try -- timeout or not.
  640. * If this doesn't succeed, then we will oops courtesy of
  641. * might_sleep() in down().
  642. */
  643. if (!down_trylock(sem))
  644. return AE_OK;
  645. switch (timeout) {
  646. /*
  647. * No Wait:
  648. * --------
  649. * A zero timeout value indicates that we shouldn't wait - just
  650. * acquire the semaphore if available otherwise return AE_TIME
  651. * (a.k.a. 'would block').
  652. */
  653. case 0:
  654. if (down_trylock(sem))
  655. status = AE_TIME;
  656. break;
  657. /*
  658. * Wait Indefinitely:
  659. * ------------------
  660. */
  661. case ACPI_WAIT_FOREVER:
  662. down(sem);
  663. break;
  664. /*
  665. * Wait w/ Timeout:
  666. * ----------------
  667. */
  668. default:
  669. // TODO: A better timeout algorithm?
  670. {
  671. int i = 0;
  672. static const int quantum_ms = 1000 / HZ;
  673. ret = down_trylock(sem);
  674. for (i = timeout; (i > 0 && ret != 0); i -= quantum_ms) {
  675. schedule_timeout_interruptible(1);
  676. ret = down_trylock(sem);
  677. }
  678. if (ret != 0)
  679. status = AE_TIME;
  680. }
  681. break;
  682. }
  683. if (ACPI_FAILURE(status)) {
  684. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
  685. "Failed to acquire semaphore[%p|%d|%d], %s",
  686. handle, units, timeout,
  687. acpi_format_exception(status)));
  688. } else {
  689. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
  690. "Acquired semaphore[%p|%d|%d]", handle,
  691. units, timeout));
  692. }
  693. return status;
  694. }
  695. EXPORT_SYMBOL(acpi_os_wait_semaphore);
  696. /*
  697. * TODO: Support for units > 1?
  698. */
  699. acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
  700. {
  701. struct semaphore *sem = (struct semaphore *)handle;
  702. if (!sem || (units < 1))
  703. return AE_BAD_PARAMETER;
  704. if (units > 1)
  705. return AE_SUPPORT;
  706. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle,
  707. units));
  708. up(sem);
  709. return AE_OK;
  710. }
  711. EXPORT_SYMBOL(acpi_os_signal_semaphore);
  712. #ifdef ACPI_FUTURE_USAGE
  713. u32 acpi_os_get_line(char *buffer)
  714. {
  715. #ifdef ENABLE_DEBUGGER
  716. if (acpi_in_debugger) {
  717. u32 chars;
  718. kdb_read(buffer, sizeof(line_buf));
  719. /* remove the CR kdb includes */
  720. chars = strlen(buffer) - 1;
  721. buffer[chars] = '\0';
  722. }
  723. #endif
  724. return 0;
  725. }
  726. #endif /* ACPI_FUTURE_USAGE */
  727. /* Assumes no unreadable holes inbetween */
  728. u8 acpi_os_readable(void *ptr, acpi_size len)
  729. {
  730. #if defined(__i386__) || defined(__x86_64__)
  731. char tmp;
  732. return !__get_user(tmp, (char __user *)ptr)
  733. && !__get_user(tmp, (char __user *)ptr + len - 1);
  734. #endif
  735. return 1;
  736. }
  737. #ifdef ACPI_FUTURE_USAGE
  738. u8 acpi_os_writable(void *ptr, acpi_size len)
  739. {
  740. /* could do dummy write (racy) or a kernel page table lookup.
  741. The later may be difficult at early boot when kmap doesn't work yet. */
  742. return 1;
  743. }
  744. #endif
  745. acpi_status acpi_os_signal(u32 function, void *info)
  746. {
  747. switch (function) {
  748. case ACPI_SIGNAL_FATAL:
  749. printk(KERN_ERR PREFIX "Fatal opcode executed\n");
  750. break;
  751. case ACPI_SIGNAL_BREAKPOINT:
  752. /*
  753. * AML Breakpoint
  754. * ACPI spec. says to treat it as a NOP unless
  755. * you are debugging. So if/when we integrate
  756. * AML debugger into the kernel debugger its
  757. * hook will go here. But until then it is
  758. * not useful to print anything on breakpoints.
  759. */
  760. break;
  761. default:
  762. break;
  763. }
  764. return AE_OK;
  765. }
  766. EXPORT_SYMBOL(acpi_os_signal);
  767. static int __init acpi_os_name_setup(char *str)
  768. {
  769. char *p = acpi_os_name;
  770. int count = ACPI_MAX_OVERRIDE_LEN - 1;
  771. if (!str || !*str)
  772. return 0;
  773. for (; count-- && str && *str; str++) {
  774. if (isalnum(*str) || *str == ' ' || *str == ':')
  775. *p++ = *str;
  776. else if (*str == '\'' || *str == '"')
  777. continue;
  778. else
  779. break;
  780. }
  781. *p = 0;
  782. return 1;
  783. }
  784. __setup("acpi_os_name=", acpi_os_name_setup);
  785. /*
  786. * _OSI control
  787. * empty string disables _OSI
  788. * TBD additional string adds to _OSI
  789. */
  790. static int __init acpi_osi_setup(char *str)
  791. {
  792. if (str == NULL || *str == '\0') {
  793. printk(KERN_INFO PREFIX "_OSI method disabled\n");
  794. acpi_gbl_create_osi_method = FALSE;
  795. } else {
  796. /* TBD */
  797. printk(KERN_ERR PREFIX "_OSI additional string ignored -- %s\n",
  798. str);
  799. }
  800. return 1;
  801. }
  802. __setup("acpi_osi=", acpi_osi_setup);
  803. /* enable serialization to combat AE_ALREADY_EXISTS errors */
  804. static int __init acpi_serialize_setup(char *str)
  805. {
  806. printk(KERN_INFO PREFIX "serialize enabled\n");
  807. acpi_gbl_all_methods_serialized = TRUE;
  808. return 1;
  809. }
  810. __setup("acpi_serialize", acpi_serialize_setup);
  811. /*
  812. * Wake and Run-Time GPES are expected to be separate.
  813. * We disable wake-GPEs at run-time to prevent spurious
  814. * interrupts.
  815. *
  816. * However, if a system exists that shares Wake and
  817. * Run-time events on the same GPE this flag is available
  818. * to tell Linux to keep the wake-time GPEs enabled at run-time.
  819. */
  820. static int __init acpi_wake_gpes_always_on_setup(char *str)
  821. {
  822. printk(KERN_INFO PREFIX "wake GPEs not disabled\n");
  823. acpi_gbl_leave_wake_gpes_disabled = FALSE;
  824. return 1;
  825. }
  826. __setup("acpi_wake_gpes_always_on", acpi_wake_gpes_always_on_setup);
  827. static int __init acpi_hotkey_setup(char *str)
  828. {
  829. acpi_specific_hotkey_enabled = FALSE;
  830. return 1;
  831. }
  832. __setup("acpi_generic_hotkey", acpi_hotkey_setup);
  833. /*
  834. * max_cstate is defined in the base kernel so modules can
  835. * change it w/o depending on the state of the processor module.
  836. */
  837. unsigned int max_cstate = ACPI_PROCESSOR_MAX_POWER;
  838. EXPORT_SYMBOL(max_cstate);
  839. /*
  840. * Acquire a spinlock.
  841. *
  842. * handle is a pointer to the spinlock_t.
  843. */
  844. acpi_cpu_flags acpi_os_acquire_lock(acpi_spinlock lockp)
  845. {
  846. acpi_cpu_flags flags;
  847. spin_lock_irqsave(lockp, flags);
  848. return flags;
  849. }
  850. /*
  851. * Release a spinlock. See above.
  852. */
  853. void acpi_os_release_lock(acpi_spinlock lockp, acpi_cpu_flags flags)
  854. {
  855. spin_unlock_irqrestore(lockp, flags);
  856. }
  857. #ifndef ACPI_USE_LOCAL_CACHE
  858. /*******************************************************************************
  859. *
  860. * FUNCTION: acpi_os_create_cache
  861. *
  862. * PARAMETERS: name - Ascii name for the cache
  863. * size - Size of each cached object
  864. * depth - Maximum depth of the cache (in objects) <ignored>
  865. * cache - Where the new cache object is returned
  866. *
  867. * RETURN: status
  868. *
  869. * DESCRIPTION: Create a cache object
  870. *
  871. ******************************************************************************/
  872. acpi_status
  873. acpi_os_create_cache(char *name, u16 size, u16 depth, acpi_cache_t ** cache)
  874. {
  875. *cache = kmem_cache_create(name, size, 0, 0, NULL, NULL);
  876. if (*cache == NULL)
  877. return AE_ERROR;
  878. else
  879. return AE_OK;
  880. }
  881. /*******************************************************************************
  882. *
  883. * FUNCTION: acpi_os_purge_cache
  884. *
  885. * PARAMETERS: Cache - Handle to cache object
  886. *
  887. * RETURN: Status
  888. *
  889. * DESCRIPTION: Free all objects within the requested cache.
  890. *
  891. ******************************************************************************/
  892. acpi_status acpi_os_purge_cache(acpi_cache_t * cache)
  893. {
  894. kmem_cache_shrink(cache);
  895. return (AE_OK);
  896. }
  897. /*******************************************************************************
  898. *
  899. * FUNCTION: acpi_os_delete_cache
  900. *
  901. * PARAMETERS: Cache - Handle to cache object
  902. *
  903. * RETURN: Status
  904. *
  905. * DESCRIPTION: Free all objects within the requested cache and delete the
  906. * cache object.
  907. *
  908. ******************************************************************************/
  909. acpi_status acpi_os_delete_cache(acpi_cache_t * cache)
  910. {
  911. kmem_cache_destroy(cache);
  912. return (AE_OK);
  913. }
  914. /*******************************************************************************
  915. *
  916. * FUNCTION: acpi_os_release_object
  917. *
  918. * PARAMETERS: Cache - Handle to cache object
  919. * Object - The object to be released
  920. *
  921. * RETURN: None
  922. *
  923. * DESCRIPTION: Release an object to the specified cache. If cache is full,
  924. * the object is deleted.
  925. *
  926. ******************************************************************************/
  927. acpi_status acpi_os_release_object(acpi_cache_t * cache, void *object)
  928. {
  929. kmem_cache_free(cache, object);
  930. return (AE_OK);
  931. }
  932. /******************************************************************************
  933. *
  934. * FUNCTION: acpi_os_validate_interface
  935. *
  936. * PARAMETERS: interface - Requested interface to be validated
  937. *
  938. * RETURN: AE_OK if interface is supported, AE_SUPPORT otherwise
  939. *
  940. * DESCRIPTION: Match an interface string to the interfaces supported by the
  941. * host. Strings originate from an AML call to the _OSI method.
  942. *
  943. *****************************************************************************/
  944. acpi_status
  945. acpi_os_validate_interface (char *interface)
  946. {
  947. return AE_SUPPORT;
  948. }
  949. /******************************************************************************
  950. *
  951. * FUNCTION: acpi_os_validate_address
  952. *
  953. * PARAMETERS: space_id - ACPI space ID
  954. * address - Physical address
  955. * length - Address length
  956. *
  957. * RETURN: AE_OK if address/length is valid for the space_id. Otherwise,
  958. * should return AE_AML_ILLEGAL_ADDRESS.
  959. *
  960. * DESCRIPTION: Validate a system address via the host OS. Used to validate
  961. * the addresses accessed by AML operation regions.
  962. *
  963. *****************************************************************************/
  964. acpi_status
  965. acpi_os_validate_address (
  966. u8 space_id,
  967. acpi_physical_address address,
  968. acpi_size length)
  969. {
  970. return AE_OK;
  971. }
  972. #endif