osl.c 25 KB

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