osl.c 25 KB

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